使用 ggpubr 包制图

简介: 使用 ggpubr 包制图

简介

Hadley Wickham撰写的ggplot2[1]是好用的软件包,是可视化工具的必备包。但是,需要知道ggplot2一定的理论与原理,对新手来说,入门门槛还是比较高的。

ggpubr软件包提供了一些易于使用的功能,基于ggplot2编写,语法十分简单的的图标。对于一些刚了解R语言,而想用R做可发表的图表的人来说,这真的太好用了!

下面对该文章ggpubr: Publication Ready Plots[2]进行讲解。

安装

  • 从CRAN安装:
install.packages("ggpubr")
  • 从GitHub安装最新版本:
if(!require(devtools))install.packages("devtools")
devtools :: install_github("kassambara / ggpubr")

可视化函数汇总

图形 命令
密度图 ggdensity()
箱型图 ggboxplot()
柱状图 gghistogram()
小提琴图 ggdotchart()
条形图 ggdotchart()
棒棒图,克利夫兰图 ggdotchart()

密度图

library(ggpubr)
# 构建数据集
set.seed(1234)
wdata = data.frame(
   sex = factor(rep(c("F", "M"), each=200)),
   weight = c(rnorm(200, 55), rnorm(200, 58)))
head(wdata, 4)

c1683668e55e3d7590d9645d6b4614d9.png


  • 密度图(ggdensity)与平均线(add = "mean");按性别("sex")进行颜色填充;加入边际地毯(rug = TRUE)并使用自定义面板(palette = c("#00AFBB", "#E7B800"))
ggdensity(wdata, x = "weight",
   add = "mean", rug = TRUE,
   color = "sex", fill = "sex",
   palette = c("#00AFBB", "#E7B800"))

974d46cbf2a0a129687cba9b8a2fa4ac.png

下面是绘制柱状图(gghistogram),其他参数和上面类似。

gghistogram(wdata, x = "weight",
   add = "mean", rug = TRUE,
   color = "sex", fill = "sex",
   palette = c("#00AFBB", "#E7B800"))

8305aa43a8374e786ff43205c6058683.png

箱型图

# Load data
data("ToothGrowth")
df <- ToothGrowth
head(df, 4)


按剂量("dose")进行颜色填充;添加抖动点并按剂量("dose")更改形状。

p <- ggboxplot(df, x = "dose", y = "len",
                color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
                add = "jitter", shape = "dose")
 p

4c33afb716a32480058263dcdbd349d7.png

还可以比较不同组均值之间的关系(stat_compare_means(label.y = 50)),并添加p值(stat_compare_means(label.y = 50) )

my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
  stat_compare_means(label.y = 50)                   # Add global p-value

b6751ac9c80607ee2ac2279731e9ea9a.png


小提琴图

下图是小提琴图(ggviolin)与箱型图的结合(add = "boxplot"),按剂量("dose")进行颜色填充;增加白色填充(add.params = list(fill = "white"))的箱型图,并加入各组比较的p值。

ggviolin(df, 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")+ # Add significance levels
  stat_compare_means(label.y = 50)                                      # Add global the p-value

15a1a7d68fe8b23cca524d0fd4757644.png

条形图

数据集

这里用mtcars数据集中进行绘制。

data("mtcars")
dfm <- mtcars
dfm$cyl <- as.factor(dfm$cyl)
dfm$name <- rownames(dfm)
head(dfm[, c("name", "wt", "mpg", "cyl")])



有序的条形图

条形图(ggbarplot)中可以利用sort.val = "desc"把数据从大到小排序并且不在组内进行排序(sort.by.groups = FALSE),而是所有数据排序;旋转x轴标签(x.text.angle = 90)。值得一提的是,这里使用了jco杂志的颜色版式(palette = "jco")。

ggbarplot(dfm, x = "name", y = "mpg",
          fill = "cyl",               # change fill color by cyl
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "desc",          # Sort the value in dscending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90           # Rotate vertically x axis texts
          )

4e8eef992852a123e218e92aa4890e03.png

如果按照组内排序的话(sort.by.groups = TRUE) ,就是下面这样。

ggbarplot(dfm, x = "name", y = "mpg",
          fill = "cyl",               # change fill color by cyl
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "asc",           # Sort the value in dscending order
          sort.by.groups = TRUE,      # Sort inside each group
          x.text.angle = 90           # Rotate vertically x axis texts
          )

f12ce3dd1f022dbba17460b9e2cbd830.png

偏差图

偏差图显示了定量值与参考值的偏差。在下面的R代码中,我们将绘制来自mtcars数据集的mpgz-score变化(标准化的一种)。

# Calculate the z-score of the mpg data
dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)
dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"),
                     levels = c("low", "high"))
head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])


根据上面的数据,创建一个有序的箱型图,按升序对值排序(sort.val = "asc")。这里和前面箱型图不同的是,使用刚建的mpg_grp变量作为填充参数,而该参数是因子(含两个水平,levels = c("low", "high"))。

ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",           # change fill color by mpg_level
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "asc",           # Sort the value in ascending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = "MPG z-score",
          xlab = FALSE,
          legend.title = "MPG Group"
          )

7c7f41965b15cac2a8596202d22e5d75.png


旋转x,y轴(rotate = TRUE)并进行降序排序(sort.val = “desc”),如下图所示,该图非常美观,可读性很强。

ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",           # change fill color by mpg_level
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "desc",          # Sort the value in descending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = "MPG z-score",
          legend.title = "MPG Group",
          rotate = TRUE,
          ggtheme = theme_minimal()
          )

散点图

棒糖图

棒糖图是条形图的另一种选择,最终图像像棒棒糖一样。

ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "ascending",                        # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           ggtheme = theme_pubr()                        # ggplot2 theme
           )


ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "descending",                       # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           rotate = TRUE,                                # Rotate vertically
           group = "cyl",                                # Order by groups
           dot.size = 6,                                 # Large dot size
           label = round(dfm$mpg),                        # Add mpg values as dot labels
           font.label = list(color = "white", size = 9,
                             vjust = 0.5),               # Adjust label parameters
           ggtheme = theme_pubr()                        # ggplot2 theme
           )


偏差图

还是使用上面的数据集,构建棒棒糖系列的偏差图。改变线段的颜色和大小: add.params = list(color = “lightgray”, size = 2)

ggdotchart(dfm, x = "name", y = "mpg_z",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "descending",                       # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           add.params = list(color = "lightgray", size = 2), # Change segment color and size
           group = "cyl",                                # Order by groups
           dot.size = 6,                                 # Large dot size
           label = round(dfm$mpg_z,1),                        # Add mpg values as dot labels
           font.label = list(color = "white", size = 9,
                             vjust = 0.5),               # Adjust label parameters
           ggtheme = theme_pubr()                        # ggplot2 theme
           )+
  geom_hline(yintercept = 0, linetype = 2, color = "lightgray")


克利夫兰散点图

在前面的基础上,只要加入以下代码即可得到克利夫兰散点图(theme_cleveland())。

ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "descending",                       # Sort value in descending order
           rotate = TRUE,                                # Rotate vertically
           dot.size = 2,                                 # Large dot size
           y.text.col = TRUE,                            # Color y text by groups
           ggtheme = theme_pubr()                        # ggplot2 theme
           )+
  theme_cleveland()                                      # Add dashed grids

参考资料

[1]

ggplot2: https://ggplot2.tidyverse.org/

[2]

ggpubr: Publication Ready Plots: http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/

相关实践学习
部署Stable Diffusion玩转AI绘画(GPU云服务器)
本实验通过在ECS上从零开始部署Stable Diffusion来进行AI绘画创作,开启AIGC盲盒。
目录
相关文章
|
机器学习/深度学习 算法 数据可视化
复杂网络-常用绘图软件和库
复杂网络-常用绘图软件和库
497 0
复杂网络-常用绘图软件和库
|
4月前
|
JSON JavaScript API
用Python编写小工具下载OSM路网数据
用Python编写小工具下载OSM路网数据
|
6月前
|
供应链 数据可视化 搜索推荐
【python plotly库介绍】从视觉到洞见:桑基图在业务分析中的应用【保姆级教程过于详细珍藏版】
【python plotly库介绍】从视觉到洞见:桑基图在业务分析中的应用【保姆级教程过于详细珍藏版】
|
7月前
|
数据可视化 API 数据库
R包:disgenet2r|DisGeNET的懒癌福利,一行代码多种可视化
DisGeNET是一个综合性的数据库,包含大量关于人类基因和疾病关联的信息,常用于生物信息学和基因组学研究。disgenet2r是R语言工具,方便用户访问和分析DisGeNET数据。用户需注册DisGeNET账号并安装R包,通过disgenet2r包可查询、检索基因-疾病关联和变异-疾病关联数据。目前DisGeNET包含超过110万个基因-疾病关联和30万个变异-疾病关联。使用示例包括查询特定疾病相关基因和多疾病联合分析。
216 0
|
数据安全/隐私保护
【实用工具指南 二】视频剪辑工具
【实用工具指南 二】视频剪辑工具
118 0
portraiture2023第三方磨皮插件下载
ps的功能这么强大,其美白磨皮方法当然不止一种。本文就给大家细数一下ps美白磨皮常用的几种方法。在各种方法中,插件法的操作更为简单,效果也更好,因此我们还会介绍ps磨皮美白插件哪个好。事不宜迟,一起来看看吧。
302 0
|
机器学习/深度学习 人工智能 算法
Portraiture2023完整版本磨皮插件下载及功能介绍
Portraiture是一款智能磨皮插件,为Photoshop和Lightroom添加一键磨皮美化功能,快速对照片中皮肤、头发、眉毛等部位进行美化,无需手动调整,大大提高P图效率。全新4版本,升级AI算法,并独家支持多人及全身模式!
820 0
|
定位技术
轻松get七普数据or制图教程
轻松get七普数据or制图教程
124 0