Skip to content

Added ternary plot for Nic #193

Merged
merged 1 commit into from Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 59 additions & 21 deletions AppRelated/LIBS_wireframe.R
Expand Up @@ -187,6 +187,46 @@ server <- function(input, output) {

# Placeholder for Ternary Plot
output$ternaryPlot <- renderPlot({# Need to figure out how to show ggtern

#Get rid of all earth samples(already done)
libswe <- libs

#Sum the elements we are looking at
libswe <- libswe %>%
mutate(y = (FeOT + MgO) / 100, z = (CaO+Na2O+K2O) / 100, x = (SiO2 + Al2O3) / 100)

# PIXL data added
pixl.df <- pixl_v1
pixl.df[sapply(pixl.df, is.character)] <- lapply(pixl.df[sapply(pixl.df, is.character)],
as.factor)
pixl.df <- pixl.df[2:16,] #Excluding first, atmospheric sample

#PIXL data, with identically reflected compositions
new_pixl_trim <- pixl.df %>%
dplyr::select(c("Na20","Mgo","Al203","Si02", "K20","Cao","FeO-T", campaign, type)) %>%
rename("Na2O"="Na20","MgO"="Mgo","Al2O3"="Al203","SiO2"="Si02","K2O"="K20",
"CaO"="Cao","FeOT"="FeO-T")

#take the sums of the specific elements, and rename type column
pixl_ternary <- new_pixl_trim %>%
mutate(x=(SiO2+Al2O3)/100,y=(FeOT+MgO)/100,z=(CaO+Na2O+K2O)/100, PIXL_Rock_Type = type) %>%
select(-c(SiO2,Al2O3,FeOT,MgO,CaO,Na2O,K2O)) %>%
drop_na()

#This is for the labels on the Ternary Plot below
pixl_ternary <- cbind(pixl_ternary, Sample_display=
c("2","3","4,6,7","5,8,9","","","","",
"10,11","","12,13","","14,15","","16"))

libs_ternplot <- libswe %>% select(c(x,y,z))

set.seed(1234)

#kmeans on the original data
tern.km <- kmeans(libs_ternplot, centers=4)

libs_ternplot <- cbind(libs_ternplot, LIBS_Cluster=as.factor(tern.km$cluster))

#ternary plot for LIBS data
#I saw that wrapping a ternary plot in a print statement helps to display the graph in r shiny, idk if it works
print(ggtern(libs_ternplot, ggtern::aes(x=x, y=y, z=z, cluster=LIBS_Cluster)) +
Expand Down Expand Up @@ -216,34 +256,32 @@ server <- function(input, output) {

# MAKE LIBS PLOTS HERE ==============================================
output$alkaliSilica_plot <- renderPlot({
suppressWarnings(
libs.uniquetar <-
aggregate(libs, list(Target = libs$Target), mean))

#Aggregate data
libs.uniquetar <- libs.uniquetar %>% select(!c(Target,Point))
libs.uniquetar <- libs.uniquetar %>%
mutate(y = (FeOT + MgO) / 100, z = (CaO+Na2O+K2O) / 100, x = (SiO2 + Al2O3) / 100)
libs_ternplot2 <- libs.uniquetar %>% select(c(x,y,z))
#Get rid of all earth samples (already done)
libswe <- libs

tern.km2 <- kmeans(as.matrix(libs_ternplot2), centers=4)
#Data Processing for the Alk Sil Plot

#Same fro aggregate data
libs_alksilag <- libs.uniquetar %>%
#Add the total alkali column (y)
libs_alksil <- libswe %>%
select(c(SiO2, TiO2, Al2O3, FeOT, MgO, CaO, Na2O, K2O)) %>%
mutate(y = Na2O + K2O)

libs_alksilag <- libs_alksilag %>%
#Drop every column except Silicon and Alkali content
libs_alksil <- libs_alksil %>%
select(c(SiO2, y))

#Cluster them according to the kmeans from the Ternary plot
libs_alksilag <- cbind(libs_alksilag, cluster=as.factor(tern.km2$cluster))
#MIGHT HAVE TO REDO THIS KMEANS, KEEP IN MIND------------------------------------------------
libs_alksil <- cbind(libs_alksil, cluster=as.factor(tern.km$cluster))

#Plot for original LIBS data
plot <- ggplot() +
geom_point(data = libs_alksilag,
ggplot() +
geom_point(data = libs_alksil,
mapping = aes(x=SiO2, y=y, color=as.character(cluster)),
alpha = 0.3) +
#add alpha so that labels on graph are visible
alpha = 0.3) +
#Add Line segments and labels for the igneous rocks reference
geom_segment(aes(x=41,y=0, xend=41, yend=7)) +
geom_segment(aes(x=45,y=0, xend=45, yend=5)) +
geom_segment(aes(x=52,y=0, xend=52, yend=5)) +
Expand Down Expand Up @@ -284,11 +322,11 @@ server <- function(input, output) {
annotate("text",x=62.5,y=11.5,label="Trachydacite", size = 2) +
annotate("text",x=56.5,y=14.5,label="Phonolite", size = 2) +
theme_minimal() +
xlim(0,78) +
ylim(-5,38) +
labs(title = "Total Alkali vs. Silica Plot for LIBS Aggregated by Target",
x = "Si",
y = "Na + K",
xlim(0,78) +
ylim(-1,20) +
labs(title = "Total Alkali vs. Silica Plot for LIBS \nWith Earth Reference Data Removed",
x = "Si",
y = "Na + K",
color="Cluster")

plot
Expand Down