#======================================================================= #Example code for how to make a violin graph in ggplot #Example data pulled from paleobiology final exam #Haleh Mawson, halehmawson@gmail.com #Dec 2020 #======================================================================= path<-"C:\\Users\\haleh\\Documents\\Biol 438 Paleobiology\\Take Home Final\\MA.vio.csv" MA.vio <- read.csv(path) #Data is leaf mass per area and time period #Goal is to show difference in means and dist. of LMA between two periods #(K & Pg) #install ggplot2 if you don't have it library(ggplot2) #load ggplot #Build the base plot vp <- ggplot(data = MA.vio, aes(x = Time, y = MA)) + geom_violin(trim = F, fill = "lightblue") #ggplot() supplies the data. Make sure it is formatted correctly #Should be in two or more adjacent columns, with no named rows #geom_violin() creates the graph #trim = F means that the plot will have gradiated edges, not sudden cut-offs #fill = color vp #check plot to make sure it worked #Add bells and whistles vp + stat_summary(fun.y=mean, geom="point", shape=20, size=4, col = "blue") + theme(panel.background = element_blank()) + ggtitle("Leaf mass per area by period") + ylab("Leaf mass per area (g/m2)") #stat_summary() adds mean (or median, or whatever) #theme() gets rid of gray background #ggtitle() sets main title #ylab() relabels y axis #For adding quartiles, include a mini boxplot in the middle vp + geom_boxplot(width=0.1, fill = "lightblue") + theme(panel.background = element_blank()) + ggtitle("Leaf mass per area by period") + ylab("Leaf mass per area (g/m2)") #These boxplots are awful because the data is awful #Or add mean and sd #install Hmisc package and it reqs library(Hmisc) vp + stat_summary(fun.data=mean_sdl, geom="pointrange", color="darkblue") + theme(panel.background = element_blank()) + ggtitle("Leaf mass per area by period") + ylab("Leaf mass per area (g/m2)") #Now you can see again how ugly the data is #Change colors by group vp <- ggplot(data = MA.vio, aes(x = Time, y = MA, fill = Time)) + geom_violin(trim = F) + theme(panel.background = element_blank()) + ggtitle("Leaf mass per area by period") + ylab("Leaf mass per area (g/m2)") vp #Now the periods are different colors #The default title is left aligned