Creating a neat and editable box and whisker plot using the ggpubr and officer packages in R

Box and whisker plots show the mean and variance for two or more groups. The plot can have jitters to show individual data points as well as it can highlight the outliers. A nice read about these plots can be found here in this Nature Method’s  note. The journal even accepted an online B/W plot maker as a publication.  These plots are often used in metabolomics publications to show differences in the levels of a metabolites across multiple groups (some plots).

These plots can be made in R using the ggplot package.  The ggpubr package enable making the publication ready B/W plots with the group comparison statistics. And with the officer package we can export the graph to an editable powerpoint for the final refining before the submission.

Here is the R code for making a box and whisker plot. Example data file is  available here bw_example.  You can convert your data into this format and easily use this code to generate b/w plot for a metabolite.

[code language=”r”]

install.packages(“pacman”)# Package manager for easy installation and loading of R-libraries.
pacman::p_load(gridExtra,ggplot2,officer,magrittr,rvg,flextable,ggplot2, ggpubr) # needed packages.

df1 <- read.delim(“bw_example.txt”, header = T, stringsAsFactors = F) #import the data file
df1$Category <- factor(df1$Category, levels=c(“CN_No”,”CN_Yes”,”LMCI_No”,”LMCI_Yes”,”AD_No”,”AD_Yes”)) ## Provide the order of the categories.
df1$Value <- as.numeric(df1$Value)
my_comparisons <- list( c(“CN_No”, “CN_Yes”), c(“LMCI_No”, “LMCI_Yes”), c(“AD_No”, “AD_Yes”), c(“CN_No”, “AD_No”), c(“CN_Yes”, “AD_Yes”) )# Add the comparison list for pvalues

p <- ggboxplot(df1, x = “Category”, y = “Value”, add = “jitter”) # use ggpubr for make the boxplot
p2 <- p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value stat_compare_means(label.y = 50) read_pptx() %>%
add_slide(layout = “Title and Content”, master = “Office Theme”) %>%
ph_with_vg(code = print(p2), type = “body”, width = 10,height = 7, offx = .3, offy = .3) %>%
print(target = “bw_plot1.pptx”) %>%
invisible()

[/code]

The output should look like –

an editable PPTx format bw_plot1 .

Happy plotting !