R语言中科研绘图神器ggpubr

2022-11-21 21:46:57 浏览数 (1)

在整理图像的时候会让人觉得苦恼的就是怎么让绘图风格统一、画风高级。今天就跟大家介绍一个画图的神器,据说可以直接赢得主编的好感。此包就是ggpubr,一个联合ggplot2包进行绘图工具包。首先看下包的安装:

代码语言:javascript复制
install.packages("ggpubr")

接下来我们通过实例来深度剖析下此包:

1. 气泡图:描述一个矩阵数据每个节点大小

代码语言:javascript复制
##载入数据
my_cols <- c("#0D0887FF", "#6A00A8FF", "#B12A90FF",
"#E16462FF", "#FCA636FF", "#F0F921FF")


# Standard contingency table
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Read a contingency table: housetasks
# Repartition of 13 housetasks in the couple
data <- read.delim(
  system.file("demo-data/housetasks.txt", package = "ggpubr"),
  row.names = 1
  )
代码语言:javascript复制
###基础绘制
ggballoonplot(data)
代码语言:javascript复制
###改变气泡的颜色
ggballoonplot(data, color = "#0073C2FF", fill = "#0073C2FF")
代码语言:javascript复制
###根据值改变颜色
ggballoonplot(data, fill = "value") scale_fill_gradientn(colors = my_cols)
代码语言:javascript复制
###改变形状
ggballoonplot(data, fill = "value",  shape = 23) gradient_fill(c("blue", "white", "red"))
代码语言:javascript复制
###固定形状大小,通过值改变颜色
ggballoonplot(data, fill = "value", color = "lightgray",size = 10, show.label = TRUE)  gradient_fill(c("blue", "white", "red"))

2. 柱状图

代码语言:javascript复制
####载入数据
df <- data.frame(dose=c("D0.5", "D1", "D2"),
   len=c(4.2, 10, 29.5))
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
   dose=rep(c("D0.5", "D1", "D2"),2),
   len=c(6.8, 15, 33, 4.2, 10, 29.5))
df3 <- ToothGrowth
###基础绘制
ggbarplot(df, x = "dose", y = "len", width = 0.5)
代码语言:javascript复制
###改变坐标轴方向
ggbarplot(df, "dose", "len", orientation = "horiz")
代码语言:javascript复制
###改变排序
ggbarplot(df, "dose", "len",order = c("D2", "D1", "D0.5"))
代码语言:javascript复制
###修改柱子颜色并添加标签
ggbarplot(df, "dose", "len",
 fill = "steelblue", color = "steelblue",
 label = TRUE, lab.pos = "in", lab.col = "white")
代码语言:javascript复制
##添加分组
ggbarplot(df, "dose", "len", color = "dose",
   palette = c("#00AFBB", "#E7B800", "#FC4E07"))
代码语言:javascript复制
###改变填充方式
ggbarplot(df, "dose", "len",
   fill = "dose", color = "dose",
   palette = c("#00AFBB", "#E7B800", "#FC4E07"))
代码语言:javascript复制
###多组数据绘制(垂直分组)
ggbarplot(df2, "dose", "len",
  fill = "supp", color = "supp", palette = "Paired",
  label = TRUE, lab.col = "white", lab.pos = "in")
代码语言:javascript复制
###多组数据绘制(水平分组)
ggbarplot(df2, "dose", "len",
  fill = "supp", color = "supp", palette = "Paired",
  label = TRUE,
  position = position_dodge(0.7))
代码语言:javascript复制
###添加误差线
ggbarplot(df3, x = "dose", y = "len", color = "supp",
 add = "mean_se", palette = c("#00AFBB", "#E7B800"),
 position = position_dodge())
代码语言:javascript复制
###添加误差线的折线图
ggline(df3, x = "dose", y = "len", color = "supp",
 add = "mean_se", palette = c("#00AFBB", "#E7B800"))
代码语言:javascript复制
###添加抖动散点折线图
ggline(df3, x = "dose", y = "len", color = "supp",
 add = c("mean_se", "jitter"), palette = c("#00AFBB", "#E7B800"))

3. 箱线图

代码语言:javascript复制
###载入数据
data("ToothGrowth")
df <- ToothGrowth


###基础绘制
ggboxplot(df, x = "dose", y = "len", width = 0.8)
代码语言:javascript复制
###修改颜色
ggboxplot(df, "dose", "len",
   color = "black", fill = "gray")
代码语言:javascript复制
###修改排序
ggboxplot(df, "dose", "len",
   order = c("2", "1", "0.5"))
代码语言:javascript复制
###改变方向
ggboxplot(df, "dose", "len", orientation = "horizontal")
代码语言:javascript复制
###添加缺口
ggboxplot(df, x = "dose", y = "len",
   notch = TRUE)
代码语言:javascript复制
###添加散点
ggboxplot(df, x = "dose", y = "len",
   add = "dotplot")
代码语言:javascript复制
###添加形状分组
ggboxplot(df, x = "dose", y = "len",
   add = "jitter", shape = "dose")
代码语言:javascript复制
###修改颜色
ggboxplot(df, "dose", "len",
    color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
add = "jitter", shape = "dose")
代码语言:javascript复制
###修改填充方式
ggboxplot(df, "dose", "len",
     fill = "dose", palette = c("#00AFBB", "#E7B800", "#FC4E07"))
代码语言:javascript复制
###多组数据绘制
ggboxplot(df, "dose", "len", color = "supp",
 palette = c("#00AFBB", "#E7B800"))
代码语言:javascript复制
###选择局部数据绘制
ggboxplot(df, "dose", "len",
   select = c("0.5", "2"))
代码语言:javascript复制
###配对箱线图
ggpaired(df3, x = "supp", y = "len",
 color = "supp", line.color = "gray", line.size = 0.4,
 palette = "npg")
代码语言:javascript复制
###添加差异分析P值
p <- ggboxplot(ToothGrowth, x = "supp", y = "len",
  color = "supp", palette = "npg", add = "jitter")
#  Add p-value
p   stat_compare_means()
代码语言:javascript复制
###添加其它分析方法
p   stat_compare_means(method = "t.test")
代码语言:javascript复制
###添加组建横线
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggboxplot(ToothGrowth, x = "dose", y = "len",
          color = "dose", palette = "npg") 
# Add pairwise comparisons p-value
stat_compare_means(comparisons = my_comparisons, label.y = c(29, 35, 40)) 
stat_compare_means(label.y = 45)     # Add global Anova p-value
代码语言:javascript复制
###设置参考组,并标注与其他组差异分析结果
ggboxplot(ToothGrowth, x = "dose", y = "len",
    color = "dose", palette = "npg") 
stat_compare_means(method = "anova", label.y = 40)  # Add global p-value
stat_compare_means(aes(label = ..p.signif..),
                  method = "t.test", ref.group = "0.5")
代码语言:javascript复制
###多组数据同时比较
p <- ggboxplot(ToothGrowth, x = "supp", y = "len",
              color = "supp", palette = "npg",
              add = "jitter",
              facet.by = "dose", short.panel.labs = FALSE)
# Use only p.format as label. Remove method name.
p   stat_compare_means(
 aes(label = paste0("p = ", ..p.format..))
)

4. 密度图绘制

代码语言:javascript复制
###数据载入
set.seed(1234)
wdata = data.frame(
   sex = factor(rep(c("F", "M"), each=200)),
   weight = c(rnorm(200, 55), rnorm(200, 58)))
###基础绘制
ggdensity(wdata, x = "weight", fill = "lightgray",
   add = "mean", rug = TRUE)
代码语言:javascript复制
###分组统计
ggdensity(wdata, x = "weight",
   add = "mean", rug = TRUE,
   color = "sex", palette = c("#00AFBB", "#E7B800"))
代码语言:javascript复制
###改变填充模式
ggdensity(wdata, x = "weight",
   add = "mean", rug = TRUE,
   color = "sex", fill = "sex",
   palette = c("#00AFBB", "#E7B800"))
代码语言:javascript复制
###密度图添加文本注释
# Density plot
density.p <- ggdensity(iris, x = "Sepal.Length",
                      fill = "Species", palette = "jco")


# Text plot
text <- paste("iris data set gives the measurements in cm",
             "of the variables sepal length and width",
             "and petal length and width, respectively,",
             "for 50 flowers from each of 3 species of iris.",
             "The species are Iris setosa, versicolor, and virginica.", sep = " ")
text.p <- ggparagraph(text, face = "italic", size = 12)


# Arrange the plots on the same page
ggarrange(density.p, text.p,
         ncol = 1, nrow = 2,
         heights = c(1, 0.3))
代码语言:javascript复制
###密度图添加表格
# Combine density plot and summary table
#:::::::::::::::::::::::::::::::::::::
# Density plot of "Sepal.Length"
density.p <- ggdensity(iris, x = "Sepal.Length",
                      fill = "Species", palette = "jco")


# Draw the summary table of Sepal.Length
# Descriptive statistics by groups
stable <- desc_statby(iris, measure.var = "Sepal.Length",
                     grps = "Species")
stable <- stable[, c("Species", "length", "mean", "sd")]
stable.p <- ggtexttable(stable, rows = NULL,
                       theme = ttheme("mOrange"))


# Arrange the plots on the same page
ggarrange(density.p, stable.p,
         ncol = 1, nrow = 2,
         heights = c(1, 0.5))
代码语言:javascript复制
###密度直方图
gghistogram(wdata, x = "weight", fill = "lightgray",
   add = "mean", rug = TRUE)
代码语言:javascript复制
##添加分组,修改颜色
gghistogram(wdata, x = "weight",
   add = "mean", rug = TRUE,
   color = "sex", fill = "sex",
   palette = c("#00AFBB", "#E7B800"))
代码语言:javascript复制
###密度曲线和直方图合并
gghistogram(wdata, x = "weight",
   add = "mean", rug = TRUE,
   fill = "sex", palette = c("#00AFBB", "#E7B800"),
   add_density = TRUE)
代码语言:javascript复制
###累计密度函数
ggecdf(wdata, x = "weight",
   color = "sex", linetype = "sex",
   palette = c("#00AFBB", "#E7B800"))
代码语言:javascript复制
###QQ图
ggqqplot(wdata, x = "weight",
   color = "sex", palette = c("#00AFBB", "#E7B800"))

5. 圆环图同饼状图(ggpie)

代码语言:javascript复制
###数据载入
df <- data.frame(
 group = c("Male", "Female", "Child"),
  value = c(25, 25, 50))
###基础绘制
ggdonutchart(df, "value", label = "group")
代码语言:javascript复制
###添加分组信息和修改颜色
# Use custom color palette
 ggdonutchart(df, "value", label = "group",
      fill = "group", color = "white",
       palette = c("#00AFBB", "#E7B800", "#FC4E07") )
代码语言:javascript复制
###修改标签内容
labs <- paste0(df$group, " (", df$value, "%)")
ggdonutchart(df, "value", label = labs,
   fill = "group", color = "white",
   palette = c("#00AFBB", "#E7B800", "#FC4E07"))
代码语言:javascript复制
###修改标签位置和颜色
ggdonutchart(df, "value", label = labs,
   lab.pos = "in", lab.font = "white",
   fill = "group", color = "white",
   palette = c("#00AFBB", "#E7B800", "#FC4E07"))

6. 点图

代码语言:javascript复制
###载入数据
data("mtcars")
df <- mtcars
df$cyl <- as.factor(df$cyl)
df$name <- rownames(df)
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
                 dose=rep(c("D0.5", "D1", "D2"),2),
                 len=c(6.8, 15, 33, 4.2, 10, 29.5))
# 基础绘制
ggdotchart(df, x = "name", y ="mpg",
  ggtheme = theme_bw())
代码语言:javascript复制
###根据分组展示不同的颜色
ggdotchart(df, x = "name", y = "mpg",
   group = "cyl", color = "cyl",
   palette = c('#999999','#E69F00','#56B4E9'),
   rotate = TRUE,
   sorting = "descending",
   ggtheme = theme_bw(),
   y.text.col = TRUE )
代码语言:javascript复制
###多组展示
ggdotchart(df2, x = "dose", y = "len",
          color = "supp", size = 3,
          add = "segment",
          add.params = list(color = "lightgray", size = 1.5),
          position = position_dodge(0.3),
          palette = "jco",
          ggtheme = theme_pubclean()
)

7. 散点图

代码语言:javascript复制
###数据载入
data("ToothGrowth")
df <- ToothGrowth


###基础绘制
ggdotplot(df, x = "dose", y = "len",
   add = "mean_sd")
代码语言:javascript复制
###添加箱线图
ggdotplot(df, x = "dose", y = "len",
 add = "boxplot")
代码语言:javascript复制
###添加分组颜色
ggdotplot(df, "dose", "len",
     add = "boxplot",
      color = "dose", fill = "dose",
      palette = c("#00AFBB", "#E7B800", "#FC4E07"))
代码语言:javascript复制
##添加小提琴图
ggdotplot(df, x = "dose", y = "len",
 add = c("violin", "mean_sd"))
代码语言:javascript复制
##多组点图
ggdotplot(df, "dose", "len", fill = "supp", color = "supp",
palette = c("#00AFBB", "#E7B800"))

8. 基因差异分布火山图

代码语言:javascript复制
###数据载入
data(diff_express)


# 基础绘制
ggmaplot(diff_express, main = expression("Group 1" %->% "Group 2"),
   fdr = 0.05, fc = 2, size = 0.4,
   palette = c("#B31B21", "#1465AC", "darkgray"),
   genenames = as.vector(diff_express$name),
   legend = "top", top = 20,
   font.label = c("bold", 11),
   font.legend = "bold",
   font.main = "bold",
   ggtheme = ggplot2::theme_minimal())
代码语言:javascript复制
###选择特别的基因展示
ggmaplot(diff_express, main = expression("Group 1" %->% "Group 2"),
         fdr = 0.05, fc = 2, size = 0.4,
         genenames = as.vector(diff_express$name),
         ggtheme = ggplot2::theme_minimal(),
         top = 0, label.select = c("BUB1", "CD83")
)

9. 散点图

代码语言:javascript复制
###载入数据
data("mtcars")
df <- mtcars
df$cyl <- as.factor(df$cyl)


###基础散点图绘制
ggscatter(df, x = "wt", y = "mpg",
   color = "black", shape = 21, size = 3, # Points color, shape and size
   add = "reg.line",  # Add regressin line
   add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line
   conf.int = TRUE, # Add confidence interval
   cor.coef = TRUE, # Add correlation coefficient. see ?stat_cor
   cor.coeff.args = list(method = "pearson", label.x = 3, label.sep = "n")
   )
代码语言:javascript复制
####基于单独函数添加相关性分析结果
sp <- ggscatter(df, x = "wt", y = "mpg",
   add = "reg.line",  # Add regressin line
   add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line
   conf.int = TRUE # Add confidence interval
   )
# Add correlation coefficient
sp   stat_cor(method = "pearson", label.x = 3, label.y = 30)
代码语言:javascript复制
###通过阈值标注P值
sp   stat_cor(p.accuracy = 0.001, r.accuracy = 0.01)
代码语言:javascript复制
###添加LOESS拟合曲线
ggscatter(df, x = "wt", y = "mpg",
   add = "loess", conf.int = TRUE)
代码语言:javascript复制
###根据值大小变化散点图
ggscatter(df, x = "wt", y = "mpg",
   color = "#00AFBB", size = "qsec")
代码语言:javascript复制
###根据不同值进行颜色变化散点图
ggscatter(df, x = "wt", y = "mpg", color = "cyl",
   palette = c("#00AFBB", "#E7B800", "#FC4E07") )
代码语言:javascript复制
###为各点添加标签
df$name <- rownames(df)
ggscatter(df, x = "wt", y = "mpg",
   color = "cyl", palette = c("#00AFBB", "#E7B800", "#FC4E07"),
   label = "name", repel = TRUE)
代码语言:javascript复制
##为分组数据添加相关性分析结果
sp <- ggscatter(df, x = "wt", y = "mpg",
   color = "cyl", palette = "jco",
   add = "reg.line", conf.int = TRUE)
sp   stat_cor(aes(color = cyl), label.x = 3)
代码语言:javascript复制
###添加密度分布的散点图
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)
)

10. 小提琴图

代码语言:javascript复制
###数据载入
data("ToothGrowth")
df <- ToothGrowth


###基础绘制
ggviolin(df, x = "dose", y = "len")
代码语言:javascript复制
###添加箱线图
ggviolin(df, x = "dose", y = "len",
 add = "boxplot")
代码语言:javascript复制
###设置颜色
ggviolin(df, "dose", "len", fill = "dose",
   palette = c("#00AFBB", "#E7B800", "#FC4E07"),
   add = "boxplot", add.params = list(fill = "white"))
代码语言:javascript复制
###多组绘制
ggviolin(df, "dose", "len", color = "supp",
 palette = c("#00AFBB", "#E7B800"), add = "boxplot")

欢迎大家学习交流!

0 人点赞