R drawing (6): saving Beginners – publication level drawing all-round package ggpubr

Time:2022-1-9

Today, I spent a lot of time sorting out the drawing functions of this package. I have to say that this package based on ggplot2 is really friendly and suitable for beginners. For those familiar with ggplot2, the existence of ggpubr may be redundant, but this does not prevent it from becoming an excellent R package.

Next, I will show about 10 functions and nearly 30 graphs of this package mainly based on variable types, covering almost most of the graphs I usually see. This tweet is also likely to be the most detailed ggpubr Chinese tutorial you’ve ever seen.
The official account is back to 20210330, obtaining today’s code and graphic example PDF.

R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

install.packages("ggpubr")
library(ggpubr)

1. Single variable – continuous

1.1 density diagram
#Create data frame
set.seed(0330)
mydata = data.frame(
  group = rep(c("A", "B","C","D"), each=200),
  value = c(rnorm(200, mean = 2), rnorm(200, 6),rnorm(200,2,4),rnorm(200,6,4))
)

ggdensity(mydata, x = "value", y="..density..", # Or ".. count..", The default is ".. density.."
          fill = "lightgray",
          Add = "mean", # or "median",
          Rug = true # adds a density line below the graph
          )
ggsave("density1.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

Group drawing

ggdensity(mydata, x = "value",
          Color = "group", fill = "group", # group
          Palette = "dark2", # or the color matching form of ggsci package such as "AAAs", or the encoding form of "#00afbb"
          add = "mean",
          rug = TRUE,
          Alpha = 0.2, # adjust transparency
          xlab=F,ylab = "Density",
          facet. By = "group", # faceted
          panel. Labels = list (group = C ("1", "2", "3", "4")), and # modify the name of each panel
         ,
          ggtheme=theme_ BW () # can be a topic type in ggplot2
)
ggsave("density2.pdf",width = 13,height = 12,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

1.2 density map superimposed normal distribution
set.seed(0330)
mydata = data.frame(
  group = rep(c("A", "B"), each=200),
  value = c(rnorm(200, 2), rnorm(200, 6, 4))
)

ggdensity(mydata, x = "value", fill = "red") +
  stat_overlay_normal_density(color = "red", linetype = "dashed")+
  scale_x_continuous(limits = c(-5,20))
ggsave("density3.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

Group drawing

ggdensity(mydata, x = "value", fill = "group") +
  stat_overlay_normal_density(aes(color=group), linetype = "dashed")+
  scale_x_continuous(limits = c(-5,20))
ggsave("density4.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

Grouping + faceting

ggdensity(mydata, x = "value", fill = "group", facet.by = "group") +
  stat_overlay_normal_density(aes(color=group), linetype = "dashed")+
  scale_x_continuous(limits = c(-5,20))
ggsave("density5.pdf",width = 16,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

1.3 histogram
gghistogram(mydata, x = "value", fill = "lightgray",
            add = "mean", rug = TRUE)
ggsave("hist1.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

Group drawing

gghistogram(mydata, x = "value", fill = "group",
            add = "mean", rug = TRUE,
            palette = c("#00AFBB", "#E7B800"))
ggsave("hist2.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

Add nuclear density map

gghistogram(mydata, x = "value", fill = "group",
            rug = TRUE,
            palette = c("#00AFBB", "#E7B800"),
            add_density = TRUE)
ggsave("hist3.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

2. Bivariate – x discrete, y continuous

2.1 box diagram
library(patchwork)
set.seed(0330)
mydata = data.frame(
  group = rep(c("A", "B"), each=100),
  group2 = rep(c("g1","g2","g1","g2"),each=50),
  value = c(rnorm(100, 2), rnorm(100, 6, 4))
)

#The following plus sign indicates the splicing figure
ggboxplot(mydata, x = "group", y = "value", width = 0.8)+
ggboxplot(mydata, x = "group", y = "value", width = 0.8, orientation = "horizontal")+
ggboxplot(mydata, x = "group", y = "value", width = 0.8, notch = TRUE,order = c("B","A"))+
ggboxplot(mydata, x = "group", y = "value", width = 0.8, select = c("A"))
ggsave("box1.pdf",width = 10,height = 10,units = "cm")

Orientation: adjust the direction of the drawing; Notch add gap; Order adjustment order; Select select a specific level to draw

R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

ggboxplot(mydata, x = "group", y = "value", width = 0.8, add = "jitter",add.params=list(color = "lightblue",size=1, shape = 17))+
ggboxplot(mydata, x = "group", y = "value", width = 0.8, add = "dotplot",add.params=list(color = "lightblue",size=0.5))
ggsave("box2.pdf",width = 16,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

Add additional graphics above
add. Params adjusts the parameters of additional graphics. Shape represents the shape of points, which can be seen in the following figure

R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

Internal grouping

ggboxplot(mydata, x = "group", y = "value", width = 0.6, color = "black",fill="group2",palette = c("#00AFBB", "#E7B800"),
          Xlab = f, # does not display the label of X axis
          bxp. errorbar=T,bxp. errorbar. Width = 0.4, # add errorbar
          Size = 1, # box type drawing edge line thickness
          outlier. Shape = Na, # do not display outlier
          Legend = "right") # legend on the right
ggsave("box3.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

2.1.1 add pairing connection to box diagram

Two data frames are acceptable

mydata2=mydata
mydata2$group2=NULL
head(mydata2)
# group    value
# 1     A 3.551687
# 2     A 3.664068
# 3     A 2.194454
# 4     A 2.569605
# 5     A 2.579997
# 6     A 1.837967
ggpaired(mydata2, x = "group", y = "value",
         color = "group", line.color = "gray", line.size = 0.4,
         palette = "npg")
                 
mydata2$id=rep(1:100,2)
mydata2=mydata2%>%reshape2::dcast(id~group)
head(mydata2)
# id        A         B
# 1  1 3.551687  4.720074
# 2  2 3.664068  7.821049
# 3  3 2.194454  8.956841
# 4  4 2.569605 -4.450063
# 5  5 2.579997  7.568216
# 6  6 1.837967  5.133688
ggpaired(mydata2, cond1 = "A", cond2 = "B",
         color = "condition", line.color = "gray", line.size = 0.4,
         palette = "npg")
ggsave("box4.pdf",width = 10,height = 10,units = "cm")

The two data frames as like as two peas are identical.

R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

2.1.2 add p value

General usage

ggboxplot(mydata, x = "group", y = "value", width = 0.8, 
          add = "dotplot",add.params=list(color = "lightblue",size=0.5))+
  stat_compare_means(method = "t.test")
ggsave("box5.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

Paired data

ggpaired(mydata2, cond1 = "A", cond2 = "B",
         color = "condition", line.color = "gray", line.size = 0.4,
         palette = "npg")+
  stat_compare_means(paired = TRUE)
ggsave("box6.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

When there are more than two groups, define the pairing to be tested

my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggboxplot(ToothGrowth, x = "dose", y = "len",
          color = "dose", palette = "npg")+
  #P value of pairwise comparison
  stat_compare_means(comparisons = my_comparisons, label.y = c(29, 35, 40))+
  #Overall p value
  stat_compare_means(label.y = 45)
ggsave("box7.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

Fix one group and compare with other groups

ggboxplot(ToothGrowth, x = "dose", y = "len",
          color = "dose", palette = "npg")+
  #Overall p value
  stat_compare_means(method = "anova", label.y = 40)+ 
  #The significance is represented by dots in label
  stat_compare_means(aes(label = ..p.signif..),
                     method = "t.test", ref.group = "0.5")
ggsave("box8.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

Compare after grouping / faceting

ggboxplot(ToothGrowth, x = "supp", y = "len",
          color = "supp", palette = "npg",
          add = "jitter",
          facet.by = "dose")+
  #Remove the inspection method from the label
  stat_compare_means(aes(label = paste0("p = ", ..p.format..)))
ggsave("box9.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

2.2 violin drawing

On the whole, the parameter options are similar to the box diagram

ggviolin(mydata, x = "group", y = "value", fill = "group",
         palette = c("#00AFBB", "#E7B800"),
         add = "boxplot", add.params = list(fill = "white"))+
Ggviolin (mydata, x = "group", y = "value", color = "group2", # internal grouping)
         palette = c("#00AFBB", "#E7B800"), 
         add = "boxplot")
ggsave("violin1.pdf",width = 16,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

2.3 column diagram
2.3.1 data have been counted
df1 <- data.frame(group=c("A", "B", "C"),
                 len=c(6, 10, 14))
ggbarplot(df1, "group", "len",
          fill = "group", color = "group",
          palette = c("#00AFBB", "#E7B800", "#FC4E07"),
          label = TRUE, lab.pos = "in", lab.col = "white")
ggsave("bar1.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

2.3.2 poor statistics

(you need a function to help you calculate, such as summation within a group, which is generally reflected in the ordinate, which is calculated by the function)

df2 <- data.frame(group=rep(c("A", "B", "C"),2),
                  group2=rep(c("1", "2"), each=3),
                  len=c(6, 15, 3, 4, 10, 5))
# group group2 len
# 1     A      1   6
# 2     B      1  15
# 3     C      1   3
# 4     A      2   4
# 5     B      2  10
# 6     C      2   5
ggbarplot(df2, "group", "len",
          fill = "group2", color = "group2", palette = "Paired",
          label = TRUE, lab.col = "white", lab.pos = "in")+
ggbarplot(df2, "group", "len",
          fill = "group2", color = "group2", palette = "Paired",
          label = TRUE,
          position = position_ Dodge (0.9)) # range 0-1, indicating the degree of staggering between columns
ggsave("bar2.pdf",width = 16,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

2.3.3 add error bar
df3 <- mydata
#Then accumulate
ggbarplot(df3, x = "group", y = "value")+
#Then find the mean
ggbarplot(df3, x = "group", y = "value",
          add = "mean")+
#Add error bar, error Plot select the display form, which is displayed up and down by default
ggbarplot(df3, x = "group", y = "value",
          add = "mean_se",
          error.plot = "upper_errorbar")+
#Internal grouping
ggbarplot(df3, x = "group", y = "value", color = "group2", 
          add = "mean_se", palette = c("#00AFBB", "#E7B800"),
          position = position_dodge())
ggsave("bar3.pdf",width = 16,height = 16,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

2.4 wiring diagram
#The data is well calculated and can be used directly
ggline(df1, x = "group", y = "len")+
#Internal grouping, dotted lines have different shapes and colors
ggline(df2, x = "group", y = "len", 
       Linetype = "group2", shape = "group2", # point shape
       color = "group2", palette = c("#00AFBB", "#E7B800"))+
#Add points and error bars
ggline(df3, x = "group", y = "value",
       add = c("mean_se","dotplot"),add.params = list(size=0.5),
       color = "steelblue")+
#For internal grouping, the color of lines is different
ggline(df3, x = "group", y = "value", color = "group2",
       add = "mean_se", palette = c("#00AFBB", "#E7B800"))
ggsave("line1.pdf",width = 16,height = 16,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

2.5 pie chart
df1$ratio=paste(df1$group,"(",round(df1$len / sum(df1$len),3) * 100,"%)",sep = "")
ggpie(df1, "len", label = "ratio",
      fill = "group", color = "white",
      palette = c("#00AFBB", "#E7B800", "#FC4E07"))+
ggpie(df1, "len", label = "ratio",
      lab.pos = "in", lab.font = "white",
      fill = "group", color = "white",
      palette = c("#00AFBB", "#E7B800", "#FC4E07"))
ggsave("pie1.pdf",width = 16,height = 10,units = "cm")

Add text annotation to label;
Color is the edge of the fan. Lab.pos adjusts the position of the text and lab.font adjusts the font color of the text

R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

2.6 ring diagram
ggdonutchart(df1, "len", label = "ratio",
             lab.pos = "in", lab.font = "white",
             fill = "group", color = "white",
             palette = c("#00AFBB", "#E7B800", "#FC4E07"))
ggsave("donut1.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

2.7 Cleveland point map
ggdotchart(df2, x = "group", y = "len",
           color = "group2", size = 3,
           add = "segment",
           add.params = list(color = "lightgray", size = 1.5),
           position = position_dodge(0.5),
           palette = "jco",
           ggtheme = theme_pubclean())
ggsave("Clevelands_Dot1.pdf",width = 10,height = 10,units = "cm")
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

3. Bivariate – x, y are continuous

3.1 adding regression line and correlation coefficient to scatter diagram
df4=mtcars
df4$cyl=as.factor(df4$cyl)
ggscatter(df4, x = "wt", y = "mpg",
          Color = "black", size = 3, # point color, size
          Add = "reg. Line", # add regression line
          add. Params = list (color = "blue", fill = "lightgray"), # regression line adjustment
          Conf.int = true, # confidence interval of regression line
          Cor.coef = true, # add correlation coefficient
          cor.coeff. Args = list (method = "Pearson", label. X = 3, label. SEP = "\ n") # correlation coefficient adjustment
)
3.2 group calculation of correlation coefficient
ggscatter(df4, x = "wt", y = "mpg",
          color = "cyl", palette = "jco",
          add = "reg.line", conf.int = TRUE)+
  stat_cor(aes(color = cyl), label.x = 3)
3.3 local regression
ggscatter(df4, x = "wt", y = "mpg",
          add = "loess", conf.int = TRUE)
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

3.4 add grouping ellipses, mean points, and radiation lines
ggscatter(df4, x = "wt", y = "mpg",
          color = "cyl", shape = "cyl",
          palette = c("#00AFBB", "#E7B800", "#FC4E07"),
          ellipse = TRUE, mean.point = TRUE,
          star.plot = TRUE)
3.5 adding text notes
df4$name <- rownames(df4)
ggscatter(df4, x = "wt", y = "mpg",
          color = "cyl", palette = c("#00AFBB", "#E7B800", "#FC4E07"),
          label = "name", repel = TRUE)+plot_layout(widths = c(1,2))
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

3.6 scatter chart edge addition density chart / box chart
ggscatterhist(
  iris, x = "Sepal.Length", y = "Sepal.Width",
  color = "Species", size = 3, alpha = 0.6,
  palette = c("#00AFBB", "#E7B800", "#FC4E07"),
  margin.params = list(fill = "Species", color = "black", size = 0.2)
)
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr

ggscatterhist(
  iris, x = "Sepal.Length", y = "Sepal.Width",
  color = "Species", size = 3, alpha = 0.6,
  palette = c("#00AFBB", "#E7B800", "#FC4E07"),
  margin.plot = "boxplot",
  ggtheme = theme_bw()
)
R drawing (6): saving Beginners - publication level drawing all-round package ggpubr


Thank you for reading here. If you have any questions, please leave a message backstage.

Due to the limited level, there are mistakes, welcome criticism and correction!