--- title: "Boxplot, Histogram, ANOVA, ggbarplot" author: "Scott Gabara" date: "06/12/2020" output: html_document: default pdf_document: default --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ## R Markdown ```{r cars} #Base graphics boxplot example #I feel like weve come a long way from base graphics and just want to give an example to highlight the difference. library(ggplot2) boxplot(PlantGrowth[,1] ~ PlantGrowth[,2], notch = TRUE, col = c(2,3,4), xlab="Treatment", ylab="Response") legend("topleft", y = 1,legend = c("setosa","versicolor","virginica"), col = c("red","green","blue"), pch=16) ``` You can embed an R code chunk like this: ```{r cars1} #ggplot2 boxplot/violin plot/jitter points example head(CO2) str(CO2) summary(CO2) CO2$TypeTreatment <- paste(CO2$Type, CO2$Treatment) ggplot(CO2, aes(x = TypeTreatment, y = uptake, fill=Type)) + geom_violin(alpha=0.5) + geom_jitter() + geom_boxplot(alpha=0.1) + stat_summary(aes(), fun.y=mean, geom="point", shape=21, size=3, fill="red",color="black", show.legend = FALSE, position=position_dodge(.75)) + scale_fill_manual(values=c("Blue", "Red")) + scale_color_manual(values=c("Blue", "Red")) + xlab("Species") + ylab(expression(~CO[2]*" uptake (umol / m"^{-2}*" sec"^{-1}*")")) + theme_classic() ``` ```{r cars2} #HISTOGRAM #A histogram of uptake using ggplot2 set to density ggplot(CO2, aes(uptake, fill = TypeTreatment)) + geom_density(alpha = 0.2) + theme_classic() # Test for homogeneity of variance b.test <- bartlett.test(CO2[,5] ~ CO2$TypeTreatment) # Bartlett test for homogeneity of variances c(b.test) summary(b.test) library(car) # load package to run leveneTest leveneTest(CO2[,5] ~ CO2$TypeTreatment) # compare Sepal length variance among species #ANOVA model1 <- lm(uptake ~ TypeTreatment, CO2) anova(model1) # Bonferroni corrected T-test bf.test <- pairwise.t.test(CO2$uptake, CO2$TypeTreatment, p.adj="bonferroni") print(bf.test) ``` ```{r cars3} # one way ANOVA with error library(Rmisc) means<-summarySE(CO2, measurevar="uptake", groupvars=c("TypeTreatment")) means # ggplot boxplot # Standard deviation of the mean as error bar p <- ggplot(CO2, aes(x=TypeTreatment, y=uptake, fill=TypeTreatment)) + geom_boxplot() + stat_summary(aes(), fun.y=mean, geom="point", shape=21, size=3, fill="red",color="black", show.legend = FALSE, position=position_dodge(.75)) + theme_classic() + ylab(expression(~CO[2]*" uptake (umol / m"^{-2}*" sec"^{-1}*")")) + scale_colour_brewer(palette = "Spectral") + scale_fill_brewer(palette = "Spectral") p ``` ```{r cars4} # Two way ANOVA boxplot with error # this example uses the free and avaiable CO2 dataset in R p <- ggplot(CO2, aes(x=Type, y=uptake, fill=Treatment)) + geom_boxplot(aes(group=TypeTreatment)) + stat_summary(aes(group=TypeTreatment), fun.y=mean, geom="point", shape=21, size=3, fill="red",color="black", show.legend = FALSE, position=position_dodge(.75)) + theme_classic() + ylab(expression(~CO[2]*" uptake (umol / m"^{-2}*" sec"^{-1}*")")) + scale_colour_manual(values = c("Red", "Blue")) + scale_fill_manual(values = c("Red", "Blue")) p ``` ```{r cars5} # Two way ANOVA barplot variant with error # this example uses the free and avaiable CO2 dataset in R library(Rmisc) qmeans<-summarySE(CO2, measurevar="uptake", groupvars=c("Type", "Treatment", "Plant")) qmeans ## We need to expand Rcolorbrewer Spectral palette size was 11 but we need 12 library(RColorBrewer) cols <- colorRampPalette(brewer.pal(12, "Spectral")) myPal <- cols(length(unique(CO2$Plant))) p <- ggplot(CO2, aes(x=Type, y=uptake, fill=Plant)) + geom_boxplot(aes(group=Plant)) + stat_summary(aes(group=Plant), fun.y=mean, geom="point", shape=21, size=3, fill="red",color="black", show.legend = FALSE, position=position_dodge(.75)) + theme_classic() + ylab(expression(~CO[2]*" uptake (umol / m"^{-2}*" sec"^{-1}*")")) + scale_fill_manual(values = myPal) + scale_color_manual(values = myPal) p ``` Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.