【生信工具】快速绘制发表级图片ggplot2智能版ggpubr包学习总结

软件简介

Hadley Wickham编写的ggplot2是出色且灵活的作图软件包,可用于R中的优雅数据可视化。ggplot2默认生成的绘图需要进行一些格式化,才能作出发表级别的图片质量。另外,要自定义使用ggplot,但其语法是不透明的,这对没有熟练R编程技能的用户增加了难度。“ ggpubr”软件包提供了一些易于使用的功能,用于创建和自定义基于“ ggplot2”的图片绘制。
ggpubr包的主要特点:

  • ggplot2软件包周围的包装,对于R编程的初学者来说,语法不太透明。
  • 使用非高级R编程技能帮助研究人员轻松创建可随时发布的图。
  • 可以将p值和显着性水平自动添加到箱形图,条形图,折线图等中。
  • 使在同一页面上排列和注释多个图变得容易。
  • 使更改颜色和标签等图形参数变得容易。

软件安装

软件安装很方便,安装完成直接加载调用

install.packages("ggpubr")
#或者
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")
library(ggpubr)
密度分布图
set.seed(1234)
df <- data.frame(sex=factor(rep(c("F","M"),each=200)),weight=c(rnorm(200,55),rnorm(200,58)))
ggdensity(df,x="weight",add = "mean",rug = TRUE,color = "sex",fill = "sex",palette= c("#00AFBB", "#E7B800"))
ggdensity
柱状图
gghistogram(df,x="weight",add = "mean",rug = TRUE,color = "sex",fill = "sex",palette = c("#00AFBB", "#E7B800"))
histogram

# Create some data format
set.seed(1234)
wdata = data.frame(sex = factor(rep(c("F", "M"), each=200)),weight = c(rnorm(200, 55), rnorm(200, 58)))
p1 <- ggqqplot(wdata, x = "weight")
p1

ggqqplot
# Change colors and shape by groups ("sex")
# Use custom palette
p2 <- ggqqplot(wdata, x = "weight",
         color = "sex", palette = c("#00AFBB", "#E7B800"))
p2
ggqqplot
# Create some data format
set.seed(1234)
wdata = data.frame(
  sex = factor(rep(c("F", "M"), each=200)),
  weight = c(rnorm(200, 55), rnorm(200, 58)))

p1 <- ggecdf(wdata, x = "weight")
p1
ggecdf
# Change colors and linetype by groups ("sex")
# Use custom palette
p2 <- ggecdf(wdata, x = "weight",
       color = "sex", linetype = "sex",
       palette = c("#00AFBB", "#E7B800"))
p2
ggecdf
p3 <- ggecdf(wdata, x = "weight", facet.by = "sex",
             color = "sex", linetype = "sex",
             palette = c("#00AFBB", "#E7B800"))
p3
ggecdf
箱线图小提琴图
rm(list = ls())
data("ToothGrowth")
df1<- ToothGrowth
head(df1)
p<- ggboxplot(df1,x="dose",y="len",color = "dose",palette=c("#00AFBB", "#E7B800", "#FC4E07"),add = "jitter",shape="dose")
p
my_comparisons <- list(c("0.5", "1"), c("1", "2"), c("0.5", "2"))
p + stat_compare_means(comparisons = my_comparisons)+ stat_compare_means(label.y = 50)
ggviolin(df1,x="dose",y="len",fill = "dose",palette = c("#00AFBB", "#E7B800", "#FC4E07"), add="boxplot", add.params=list(fill="white")) + stat_compare_means(comparisons = my_comparisons, label = "p.signif") + stat_compare_means(label.y = 50)

ggboxplot

ggboxplot

ggviolin
柱状图
data("mtcars")
df2<- mtcars
df2$cyl <- factor(df2$cyl)
df2$name <- rownames(df2)
head(df2[,c("name","wt","mpg")])
ggbarplot(df2,x="name",y="mpg",fill = "cyl",color = "white",palette="npg",sort.val = "desc",sort.by.groups = FALSE,x.text.angle= 60)
ggbarplot
data("mtcars")
df2<- mtcars
df2$cyl <- factor(df2$cyl)
df2$name <- rownames(df2)
head(df2[,c("name","wt","mpg")])
ggbarplot(df2,x="name",y="mpg",fill = "cyl",color = "white",palette="aaas",sort.val = "asc",sort.by.groups = TRUE,x.text.angle= 60)
ggbarplot
df2$mpg_z <- (df2$mpg - mean(df2$mpg))/sd(df2$mpg)

df2$mpg_grp <- factor(ifelse(df2$mpg_z<0,"low","high"),levels = c("low","high"))
head(df2[,c("name","wt","mpg","mpg_grp","cyl")])
ggbarplot(df2,x="name",y="mpg_z",fill = "mpg_grp",color = "white",palette="jco",sort.val = "asc",sort.by.groups = FALSE,x.text.angle=60,ylab="MPG z-score",xlab = FALSE,legend.title="MPG Group", rotate=TRUE)
ggbarplot
ggdotchart(df2,x="name",y="mpg",color="cyl",palette=c("#00AFBB", "#E7B800", "#FC4E07"),sorting = "ascending",label=round(df2$mpg),sort.by.groups=TRUE, add = "segments",ggtheme = theme_pubr())
ggdotchart
散点图
data("mtcars")
df <- mtcars
df$cyl <- as.factor(df$cyl)
head(df)
p1 <- ggscatter(df, x = "wt", y = "mpg",
                color = "red")
p1
ggscatter
p2 <- 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")
)
p2
ggscatter
# loess method: local regression fitting
p3 <- ggscatter(df, x = "wt", y = "mpg",
          add = "loess", conf.int = TRUE,
          cor.coef = TRUE, # Add correlation coefficient. see ?stat_cor
          cor.coeff.args = list(method = "spearman", label.x = 3, label.sep = "\n")
)
p3
ggscatter
# Control point size by continuous variable values ("qsec")
p4 <- ggscatter(df, x = "wt", y = "mpg",
          color = "#00AFBB", size = "qsec")
p4
ggscatter
# Change colors
# Use custom color palette
# Add marginal rug
p5 <- ggscatter(df, x = "wt", y = "mpg", color = "cyl", size = "qsec",
          palette = c("#00AFBB", "#E7B800", "#FC4E07") )
p5

ggscatter
p6 <- ggscatter(df, x = "wt", y = "mpg", color = "cyl", rug=TRUE,
                palette = c("#00AFBB", "#E7B800", "#FC4E07") )
p6
ggscatter
# Add group ellipses and mean points
# Add stars
p7 <- ggscatter(df, x = "wt", y = "mpg",
          color = "cyl", shape = "cyl",
          palette = c("#00AFBB", "#E7B800", "#FC4E07"),
          ellipse = TRUE)
p7

ggscatter
p8 <- ggscatter(df, x = "wt", y = "mpg",
                color = "cyl", shape = "cyl",
                palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                ellipse = TRUE, ellipse.type = "convex",
                mean.point = TRUE,
                )
p8

ggscatter
p9 <- ggscatter(df, x = "wt", y = "mpg",
                color = "cyl", shape = "cyl",
                palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                ellipse = TRUE, ellipse.type = 'confidence',
                mean.point = TRUE,
                star.plot = TRUE)
p9

ggscatter
# Textual annotation
df$name <- rownames(df)
p10 <- ggscatter(df, x = "wt", y = "mpg",
          color = "cyl", palette = c("#00AFBB", "#E7B800", "#FC4E07"),
          label = "name")
p10

ggscatter
p11 <- ggscatter(df, x = "wt", y = "mpg",
                color = "cyl", palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                label = "name", repel = TRUE)
p11
ggscatter
# Data
df <- data.frame(dose=c("D0.5", "D1", "D2"),
                 len=c(4.2, 10, 29.5))
print(df)

p1 <- ggbarplot(df,x="dose",y="len",label=TRUE,label.pos="out")
p1
ggbarplot
# Change width 更改柱子的宽度
p2 <- ggbarplot(df, x = "dose", y = "len", width = 0.2)
p2
ggbarplot
# Change the plot orientation: horizontal 变换坐标轴的方向
p3 <- ggbarplot(df, "dose", "len", orientation = "horiz")
p3 
ggbarplot
# Change the default order of items 设定指定的排列顺序
p4 <- ggbarplot(df, "dose", "len",
          order = c("D2", "D1", "D0.5"))
p4
ggbarplot
# Change colors 更改填充色和边框色
# Change fill and outline color
# add labels inside bars
p5 <- ggbarplot(df, "dose", "len",
          fill = "steelblue", color = "black",
          label = TRUE, lab.pos = "in", lab.col = "white")
p5
ggbarplot
# Change colors by groups: dose
# Use custom color palette
p6 <- ggbarplot(df, "dose", "len", color = "dose",
          palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p6
ggbarplot
# Change fill and outline colors by groups
p7 <- ggbarplot(df, "dose", "len",
          fill = "dose", color = "dose",
          palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p7
ggbarplot
# Plot with multiple groups 分组绘图
# Create some data
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))
print(df2)

# Plot "len" by "dose" and change color by a second group: "supp"
# Add labels inside bars
p8 <- ggbarplot(df2, "dose", "len",
          fill = "supp", color = "supp",
          label = TRUE, lab.col = "white", lab.pos = "in")
p8
ggbarplot
# Change position: Interleaved (dodged) bar plot 更改排列方式
p9 <- ggbarplot(df2, "dose", "len",
          fill = "supp", color = "supp", palette = "Paired",
          label = TRUE,
          position = position_dodge())
p9
ggbarplot
p10 <- ggbarplot(df2, "dose", "len",
                fill = "supp", color = "supp", palette = "Paired",
                label = TRUE,
                position = position_fill())
p10
ggbarplot
# Add points and errors
# Data: ToothGrowth data set we'll be used.
df3 <- ToothGrowth
head(df3, 10)
# It can be seen that for each group we have
# different values
p11 <- ggbarplot(df3, x = "dose", y = "len",width =0.3)
p11
ggbarplot
# Visualize the mean of each group
p12 <- ggbarplot(df3, x = "dose", y = "len",
          add = "mean")
p12
ggbarplot
# Add error bars: mean_se 添加不同类型的误差棒
# (other values include: mean_sd, mean_ci, median_iqr, ....)
# Add labels
p13 <- ggbarplot(df3, x = "dose", y = "len",
          add = "mean_se", label = TRUE, lab.vjust = -1.6)
p13
误差棒
# Use only "upper_errorbar"
p14 <- ggbarplot(df3, x = "dose", y = "len",
          add = "mean_se", error.plot = "upper_errorbar")
p14
头部误差棒
# Change error.plot to "pointrange"
p15 <- ggbarplot(df3, x = "dose", y = "len",
          add = "mean_se", error.plot = "pointrange")
p15
误差点
# Add jitter points and errors (mean_se)
p16 <- ggbarplot(df3, x = "dose", y = "len",
          add = c("mean_se", "jitter"))
p16
误差散点
# Add dot and errors (mean_se)
p17 <- ggbarplot(df3, x = "dose", y = "len",
          add = c("mean_se", "dotplot"))
p17
误差点
# Multiple groups with error bars and jitter point
p18 <- ggbarplot(df3, x = "dose", y = "len", color = "supp",
          add = "mean_se", palette = c("#00AFBB", "#E7B800"),
          position = position_dodge())
p18
误差点
MA图
# 加载数据集
data(diff_express)
head(diff_express)
p1 <- ggmaplot(diff_express, fdr = 0.05, fc = 2, size = 0.4,
               palette = c("red","green","gray"))
p1
MA图
p2 <- 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),
         xlab = "M",ylab = "A",
         legend = "top", top = 20,
         font.label = c("bold", 11),
         font.legend = "bold",
         font.main = "bold",
         ggtheme = ggplot2::theme_minimal())
p2
MA图
# Add rectangle around labels
p3 <- 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), label.rectangle = TRUE,
         font.legend = "bold", select.top.method = "padj",
         font.main = "bold",
         ggtheme = ggplot2::theme_minimal())
p3
MA图
p4 <- ggmaplot(diff_express, main = expression("Group 1" %->% "Group 2"),
               fdr = 0.05, fc = 2, size = 0.5,
               palette = c("#B31B21", "#1465AC", "darkgray"),
               genenames = as.vector(diff_express$name),
               legend = "top", top = 20, select.top.method = "fc",
               font.label = c("bold", 11), label.rectangle = TRUE,
               font.legend = "bold",
               font.main = "bold",
               ggtheme = ggplot2::theme_minimal())
p4
MA图
饼状图
# Data: Create some data
df <- data.frame(
  group = c("Male", "Female", "Child"),
  value = c(25, 25, 50))
head(df)

p1 <- ggpie(df, "value", label = "group")
p1
饼状图
# Change color
# Change fill color by group
# set line color to white
# Use custom color palette
p2 <- ggpie(df, "value", label = "group",
      fill = "group", color = "white",
      palette = c("#00AFBB", "#E7B800", "#FC4E07") )
p2
饼状图
# Change label
# Show group names and value as labels
labs <- paste0(df$group, " (", df$value, "%)")
labs

p3 <- ggpie(df, "value", label = labs,
      fill = "group", color = "white",
      palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p3
饼状图
# Change the position and font color of labels
p4 <- ggpie(df, "value", label = labs,
      lab.pos = "in", lab.font = "white",
      fill = "group", color = "white",
      palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p4
饼状图
参考材料:
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,014评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,796评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,484评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,830评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,946评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,114评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,182评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,927评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,369评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,678评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,832评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,533评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,166评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,885评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,128评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,659评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,738评论 2 351