how to use ggpubr plot various kinds of box plots

created at 06-30-2021 views: 2

Today we continue to learn ggpubr package to draw various SCI publication-level box plots.

library(ggpubr)
library(patchwork) # If not installed, install it first
set.seed(123) # Set the number of seeds

First generate a random data set, the data set has 300 observations, 3 variables (respectively A, sex, smokestatus)

dataset=data.frame(A=c(rnorm(150,12,2), rnorm(150, 6,1)),       sex=sample(c("0","1"),300,replace=TRUE),       smokestatus=sample(c("1","2","3"),300,replace=TRUE))

simple box diagram

(the plus sign below indicates splicing graphics, if splicing is not required, you can directly remove the plus sign)

ggboxplot(dataset, x = "smokestatus", y = "A", bxp.errorbar=T,width = 0.5, color = "smokestatus",  palette="aaas", )+ggboxplot(dataset, x = "smokestatus", y = "A", width = 0.5, color = "smokestatus",        palette="aaas",bxp.errorbar=T,orientation = "horizontal" )+ggboxplot(dataset, x = "smokestatus", y = "A", width = 0.5,color = "smokestatus",        palette="aaas",bxp.errorbar=T,notch = TRUE, order = c("3","2","1") )+ggboxplot(dataset, x = "smokestatus", y = "A", width = 0.5, color = "smokestatus", palette="aaas",bxp.errorbar=T, select = c("3") )

simple box diagram

box plot with scattered points

ggboxplot(dataset, x = "smokestatus", y = "A", width = 0.8, add = "jitter", add.params=list(color = "smokestatus",size=0.8, shape = 23)) 

box plot with scattered points

clustered box plot

the abscissa represents smokestatus grouping, and different colors represent sex of different genders

ggboxplot(dataset, x = "smokestatus", y = "A", width = 0.6,       color = "black", fill="sex", palette =c("#E7B800", "#00AFBB"), xlab = F, bxp.errorbar=T, bxp.errorbar.width=0.5, size=1, outlier.shape=NA, outlier      legend = "right")  

Add three sets of overall P values to the box plot

ggboxplot(dataset, x = "smokestatus", y = "A", width = 0.8, add = "jitter",add.params=list(color = "smokestatus",size=0.5))+ stat_compare_means(method = "anova")

Pairwise comparison

my_comparisons <- list( c("1", "2"), c("1", "3"), c("3", "2") )ggboxplot(dataset, x = "smokestatus", y = "A",      color = "smokestatus", palette = "npg")  stat_compare_means(comparisons = my_comparisons, label.y = c(18, 22, 26))+ stat_compare_means(label.y = 28)

Fix a certain group, compare the others with a certain group

ggboxplot(dataset, x = "smokestatus", y = "A", color = "smokestatus", palette = "npg")+ stat_compare_means(method = "anova", label.y = 28)+ stat_compare_means(method = "t.test", ref.group = "1")

Compare after grouping/faceting

ggboxplot(dataset, x = "sex", y = "A",      color = "sex", palette = "npg",      add = "jitter",  facet.by = "smokestatus")+stat_compare_means(aes(label = paste0("p = ", ..p.format..)))

That's all for today's study.

Please log in to leave a comment.