head(ToothGrowth)
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
library(ggpubr)
## Loading required package: ggplot2
## Loading required package: magrittr
MyComparisons = list(c("0.5", '1'), c('1', '2'), c("0.5", "2"))
MyPlot = ggboxplot(data = ToothGrowth, x = 'supp', y = 'len', fill = 'supp', palette = 'Dark2', xlab = "Dose", ylab = "Length (nm)", width = .5) + facet_wrap(.~dose) + stat_compare_means(comparisons = MyComparisons, bracket.size = .4, size = 8) + stat_compare_means(label.y = 50, method = 't.test', size = 3) + theme(legend.position = 'none')
MyPlot
## Warning: Computation failed in `stat_signif()`:
## missing value where TRUE/FALSE needed
## Warning: Computation failed in `stat_signif()`:
## missing value where TRUE/FALSE needed
## Warning: Computation failed in `stat_signif()`:
## missing value where TRUE/FALSE needed
# The basics of a plot with ggpubr, there is no need to create a base, and the nasdd layers to it, instead we can just jump in to ggboxplot, or ggscatter etc.
## The basic bones of the
ggboxplot(data = ToothGrowth, x = 'dose', y = 'len', xlab = "Dose", ylab = "Length (nm)")
## Add some colors if you wish, but keep in mind how it will look on a black and white printer. Here is fow you FILL with arbitrary colors
ggboxplot(data = ToothGrowth, x = 'dose', y = 'len', xlab = "Dose",fill = 'dose', ylab = "Length (nm)")
## You can also leave them unfilled if you like, but color them nontheless
ggboxplot(data = ToothGrowth, x = 'dose', y = 'len', xlab = "Dose",color = 'dose', ylab = "Length (nm)")
## If you wish to control the colors in your plot, you can schoose them yourself, or select a premade "pallette", which has an assotment of colors aleady within it. Here is an example of both.
# Choosing the colors myself
ggboxplot(data = ToothGrowth, x = 'dose', y = 'len', xlab = "Dose",color = 'dose', palette = c('yellow', 'black', 'magenta' ), ylab = "Length (nm)")
# Choosing a pallette
ggboxplot(data = ToothGrowth, x = 'dose', y = 'len', xlab = "Dose",color = 'dose', palette = 'Dark2', ylab = "Length (nm)")
# I used Dark2 but you can use any of the palletees you want, just google R Pallette and pages with their names and resulting colors will appear.
# Avoid the problem of black and white printing, with a gray fill.
ggboxplot(data = ToothGrowth, x = 'dose', y = 'len',fill = 'dose', xlab = "Dose", ylab = "Length (nm)") + scale_fill_grey(start = .9, end = .3)
MyComparisons = list( c('0.5', '1'), c('1', '2'), c('0.5', '2'))
ggboxplot(data = ToothGrowth, x = 'dose', y = 'len', fill = 'dose') +
stat_compare_means(comparisons = MyComparisons, bracket.size = .6, size = 4)
#### So now that we have added comparative statistics, lets add an overall test statistic. #### In this case I will use ANOVA
MyComparisons = list( c('0.5', '1'), c('1', '2'), c('0.5', '2'))
ggboxplot(data = ToothGrowth, x = 'dose', y = 'len', fill = 'dose') +
stat_compare_means(comparisons = MyComparisons, bracket.size = .6, size = 4) +
stat_compare_means(label.y = 45, method = "anova")
#### Do we really need a legend?
MyComparisons = list( c('0.5', '1'), c('1', '2'), c('0.5', '2'))
ggboxplot(data = ToothGrowth, x = 'dose', y = 'len', fill = 'dose') +
stat_compare_means(comparisons = MyComparisons, bracket.size = .6, size = 4) +
stat_compare_means(label.y = 45, method = "anova") + theme(legend.position = 'none')
#The comparisons are no longer needed, so lets comment these out.
#MyComparisons = list( c('0.5', '1'), c('1', '2'), c('0.5', '2'))
ggboxplot(data = ToothGrowth, x = 'supp', y = 'len', color = 'supp') + facet_wrap(.~dose) + stat_compare_means(label.y = 35, method = "wilcox.test") + theme(legend.position = 'none')
MyComparisons = list( c('0.5', '1'), c('1', '2'), c('0.5', '2'))
ggboxplot(data = ToothGrowth, x = 'dose', y = 'len', fill = 'dose') +
stat_compare_means(comparisons = MyComparisons, symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), symbols = c("****", "***", "**", "*", "ns"))) +
stat_compare_means(label.y = 45, method = "anova")
MyComparisons = list( c('0.5', '1'), c('1', '2'), c('0.5', '2'))
ggboxplot(data = ToothGrowth, x = 'dose', y = 'len', fill = 'dose') +
#We add two more arguments
stat_compare_means(comparisons = MyComparisons, symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), symbols = c("****", "***", "**", "*", "ns")), bracket.size = .6, size = 8) +
stat_compare_means(label.y = 45, method = "anova")
###### Continue tweakign those two argunents until you have waht you need.
# We have to assign our plot to a variable and then send that variable through ggpar. Take a look at the 'p' in the below plot
p = ggboxplot(data = ToothGrowth, x = 'dose', y = 'len', color = 'dose', palette = c("red", "blue", "black"), xlab = "Suppliment", ylab = "Length", width = .4) + stat_compare_means(comparisons = MyComparisons, symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), symbols = c("****", "***", "**", "*", "ns"), method = 't.test')) + stat_compare_means(label.y = 45, size = 3, label.x = .9) +
theme(legend.position = 'none')
# Reassign p with the new graphical parameters
p = ggpar(p, ticks = FALSE, title = "Plot for Class", font.main = c(19, "plain", "black"), font.xtickslab = c(12, "plain", "blue"))
p