Boxplot using R [Geochemistry][Geology][Petrology]





Data source: Compositional Data Analysis (CoDA) of Clinopyroxene from Abyssal Peridotites



Library

library(dplyr)
library(ggplot2)
library(tidyverse)
library(RColorBrewer)
library(viridis) 

library(ggdark)


Read files and exclude DMM data

data_new <- read.csv("residual_abyssal_peridotites.csv")
data_new <- data_new %>% filter(!(Ridge == "DMM"))


Visualization 1

crspl <- ggplot(data_new)+
  geom_boxplot(aes(x=Ridge, y=CrSPL))
plot(crspl)



Changing the theme and the y-axis name

crspl <- ggplot(data_new)+
  geom_boxplot(aes(x=Ridge, y=CrSPL))
crspl <- crspl + theme_bw() + ylab("Cr# of spinel")
plot(crspl)


Adding colors

crspl <- ggplot(data_new)+
  geom_boxplot(aes(x=Ridge, y=CrSPL, fill = Ridge), color = "black")
crspl <- crspl + theme_bw() + ylab("Cr# of spinel")
plot(crspl)



Changing colors

crspl <- ggplot(data_new)+
  geom_boxplot(aes(x=Ridge, y=CrSPL, fill = Ridge), color = "black")
crspl <- crspl + theme_bw() + ylab("Cr# of spinel")
crspl <- crspl + scale_fill_manual(values = c("AAR" = "green4",
                                        "EPR" = "sienna4",
                                        "CIR" = "orangered1",
                                        "MAR" = "orange1",
                                        "SWIR" = "royalblue4",
                                        "LT" = "lightskyblue3",
                                        "GAK" = "royalblue1"))
plot(crspl)


Adding the points (jitter plots) on top of the box plot

crspl <- ggplot(data_new)+
  geom_boxplot(aes(x=Ridge, y=CrSPL, fill = Ridge), color = "black")
crspl <- crspl + theme_bw() + ylab("Cr# of spinel")
crspl <- crspl + scale_fill_manual(values = c("AAR" = "green4",
                                        "EPR" = "sienna4",
                                        "CIR" = "orangered1",
                                        "MAR" = "orange1",
                                        "SWIR" = "royalblue4",
                                        "LT" = "lightskyblue3",
                                        "GAK" = "royalblue1"))
crspl <- crspl + geom_jitter(aes(x=Ridge, y=CrSPL), color = "black", width=0.3) 
plot(crspl)




Changing colors

crspl <- ggplot(data_new)+
  geom_boxplot(aes(x=Ridge, y=CrSPL), color = "black", alpha = 0)
crspl <- crspl + theme_bw() + ylab("Cr# of spinel") 
crspl <- crspl + geom_jitter(aes(x=Ridge, y=CrSPL, color = Ridge), width=0.3)
crspl <- crspl + scale_color_manual(values = c("AAR" = "green4",
                                        "EPR" = "sienna4",
                                        "CIR" = "orangered1",
                                        "MAR" = "orange1",
                                        "SWIR" = "royalblue4",
                                        "LT" = "lightskyblue3",
                                        "GAK" = "royalblue1"))
plot(crspl)




Editing axis

scale_y_continuous(breaks=seq(0,100,20), limits=c(0,100))

crspl <- ggplot(data_new)+
  geom_boxplot(aes(x=Ridge, y=CrSPL), color = "black", alpha = 0)
crspl <- crspl + theme_bw() + ylab("Cr# of spinel") + scale_y_continuous(breaks=seq(0,100,20), limits=c(0,100))
crspl <- crspl + geom_jitter(aes(x=Ridge, y=CrSPL, color = Ridge), width=0.3)
crspl <- crspl + scale_color_manual(values = c("AAR" = "green4",
                                        "EPR" = "sienna4",
                                        "CIR" = "orangered1",
                                        "MAR" = "orange1",
                                        "SWIR" = "royalblue4",
                                        "LT" = "lightskyblue3",
                                        "GAK" = "royalblue1"))
plot(crspl)




Changing theme

crspl <- ggplot(data_new)+
  geom_boxplot(aes(x=Ridge, y=CrSPL), alpha =0)+
  geom_jitter(aes(x=Ridge, y=CrSPL, color = Ridge), width=0.3, alpha =0.6) + dark_theme_gray() + ylab("Cr# of spinel") + scale_y_continuous(breaks=seq(0,100,20), limits=c(0,100))
crspl <- crspl + scale_colour_manual(values = c("AAR" = "green4",
                                        "EPR" = "sienna4",
                                        "CIR" = "orangered1",
                                        "MAR" = "orange1",
                                        "SWIR" = "royalblue4",
                                        "LT" = "lightskyblue3",
                                        "GAK" = "royalblue1"))
plot(crspl)






keywords: abyssal peridotites, clinopyroxene, box plot, spinel, clinopyroxene, rstats, r, ggplot, Geochemistry, Geology, Petrology, using R, r bloggers