Creating Publication Plots in R

Introduction to using ggpubr

Please excuse any spelling errors, as R Markdowns do not have spell check.
To create a publication plot, it is important to remember that your individual jounral will have a checklist of its own. Here I create a publication ready plot under the checklist of a jounal called Global Change Biology
The data we will use today is ToothGrowth
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

Anlayze the data set.

This data set follows the tooth growth of ginea pigs given Vitamin C over two dilevery methods and three doses.
We want to create a publication plot showing comparative means between the methods and doses.
Lets get to it

ggpubr vs. ggplot2

The first thing we will do is change over from the ggplot2 method to a new package called ggpubr, a graphing package made to create plotting easier for those with no advances coding expereince.
Lets get to it
PLease look at teh below code, it is the finished code to create a plot realtivley ready to be touched up for publication. It si the end goal and we will work step by step to get there.
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

Lets start with the basics…

Adding color to your plots…things to think about (please read all teh comments below.

# 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)

Adding to the plot, first we put up comparative statistics with the stat_compare_means() layer

Remember if you want to run morea than one test, you have to use stat_compare_means twice

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')

Here we are looking at the three doses but not the actual delivery vehicles, we need to FACET, take a look

#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')

In some cases, publications do not want the test-statistics on the actual plot, but instead aserixes, you have to define what each aserix means. Sheck it out below, its just another set of arguments.
Lets go back to comparing the doses
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")

Ok….. so we kinda got we wanted but lets add a little more customability
Lets try that again.
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.

What else can we do to the graph to make sure it fits the publication jounals “checklist?”
Well that would depend, but no matter what, we would use the ggpar package!
# 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

What you input into the ggpar function depends entirely on the “checklist” your publication wants.