原文链接:ggpubr: ‘ggplot2’ Based Publication Ready Plots
1.ggpubr
Hadley Wickham编写的ggplot2是出色且灵活的软件包,可用于R中的优雅数据可视化。但是,默认生成的绘图必须先进行一些格式化,然后才能发送它们进行发布。 此外,要自定义ggplot,语法是不透明的,这对没有高级R编程技能的研究人员增加了难度。
“ ggpubr”软件包提供了一些易于使用的功能,用于创建和自定义基于“ ggplot2”的可发布出版物的图表。
在https://rpkgs.datanovia.com/ggpubr上可找到更多信息。
幕布链接:https://share.mubu.com/doc/6kFLvwuA6he
2.ggpubr安装及加载
2.1 从CRAN安装
install.packages("ggplot2")
install.packages("ggpubr")
2.2 从Github安装最新版
# Install
if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")
3.分布图 Distribution
library(ggpubr)
#>加载所需的包: ggplot2
#>加载所需的包: magrittr
# 创建数据集
set.seed(1234)
wdata = data.frame(
sex = factor(rep(c("F", "M"), each=200)),
weight = c(rnorm(200, 55), rnorm(200, 58)))
# head(dataframe),默认查看数据集前6行数据
head(wdata, 4)
# 带有平均线和边际地毯的密度图
# 按组更改轮廓和填充颜色(“性别”)
# 使用自定义调色板
ggdensity(wdata, x = "weight",
add = "mean", rug = TRUE,
color = "sex", fill = "sex",
palette = c("#00AFBB", "#E7B800"))
#带有平均线和边缘地毯的直方图
#按组更改轮廓和填充颜色(“性别”)
#使用自定义调色板
gghistogram(wdata, x = "weight",
add = "mean", rug = TRUE,
color = "sex", fill = "sex",
palette = c("#00AFBB", "#E7B800"))
4.箱形图和小提琴图 Box plots and violin plots
# 加载数据
data("ToothGrowth")
df <- ToothGrowth
# head(dataframe),默认查看数据集前6行数据
head(df, 4)
#带有抖动点的箱形图
#按组更改轮廓颜色:剂量
#使用自定义调色板
#添加抖动点并按组更改形状
p <- ggboxplot(df, x = "dose", y = "len",
color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
add = "jitter", shape = "dose")
p
# 添加p值比较组
# 指定所需的比较
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
p + stat_compare_means(comparisons = my_comparisons)+
#添加成对比较p值
stat_compare_means(label.y = 50)
# 添加全局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")+
# 添加显著性等级
stat_compare_means(label.y = 50)
# 添加全局p值
5.条形图 Bar plots
- Demo data set
# 加载数据
data("mtcars")
dfm <- mtcars
# 将cyl变量转换为因子
dfm$cyl <- as.factor(dfm$cyl)
# 添加组名
dfm$name <- rownames(dfm)
# 检查数据
head(dfm[, c("name", "wt", "mpg", "cyl")])
- Ordered bar plots
通过分组变量“ cyl”更改填充颜色。 排序将在全局范围内进行,但不会按组进行。
ggbarplot(dfm, x = "name", y = "mpg",
fill = "cyl", # 通过cyl更改填充颜色
color = "white", # 将条形边框颜色设置为白色
palette = "jco", # jco journal color palett. see ?ggpar
sort.val = "desc", # 以升序对值进行排序
sort.by.groups = FALSE, # 不要在每个组内排序
x.text.angle = 90 # 垂直旋转x轴文本
)
ggbarplot(dfm, x = "name", y = "mpg",
fill = "cyl", # 通过cyl更改填充颜色
color = "white", # 将条形边框颜色设置为白色
palette = "jco", # jco journal color palett. see ?ggpar
sort.val = "asc", # 按降序对值排序
sort.by.groups = TRUE, # 在每个组内排序
x.text.angle = 90 # 垂直旋转x轴文本
6.偏差图 Deviation graphs
偏差图显示定量值与参考值的偏差。 在下面的R代码中,我们将绘制mtcars数据集中的mpg z得分。
计算mpg数据的z得分:
# 计算mpg的z-score
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")])
# 创建一个有序的条形图,根据mpg的级别进行着色:
ggbarplot(dfm, x = "name", y = "mpg_z",
fill = "mpg_grp", # 通过mpg_level更改填充颜色
color = "white", # 将条形边框颜色设置为白色
palette = "jco", # jco journal color palett. see ?ggpar
sort.val = "asc", # 按降序对值排序
sort.by.groups = FALSE, # 不要在每个组内排序
x.text.angle = 90, # 垂直旋转x轴文本
ylab = "MPG z-score",
xlab = FALSE,
legend.title = "MPG Group"
旋转绘图:使用rotate = TRUE和sort.val =“ desc”
ggbarplot(dfm, x = "name", y = "mpg_z",
fill = "mpg_grp", # 通过mpg_level更改填充颜色
color = "white", # 将条形边框颜色设置为白色
palette = "jco", # jco journal color palett. see ?ggpar
sort.val = "asc", # 按降序对值排序
sort.by.groups = FALSE, # 不要在每个组内排序
x.text.angle = 90, # 垂直旋转x轴文本
ylab = "MPG z-score",
legend.title = "MPG Group",
rotate = TRUE,
ggtheme = theme_minimal()
7.点图 Dot charts
- 棒棒糖图
当您具有大量要可视化的值时,棒棒糖图表可以代替条形图。
棒棒糖图表由分组变量“ cyl”着色
ggdotchart(dfm, x = "name", y = "mpg",
color = "cyl", # 按组区分颜色
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # 自定义调色板
sorting = "ascending", # 按降序对值进行排序
add = "segments", # 将y = 0的段添加到点
ggtheme = theme_pubr() # 选择ggplot2主题
ggdotchart(dfm, x = "name", y = "mpg",
color = "cyl", # 按组区分颜色
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # 自定义调色板
sorting = "descending", # 按降序对值进行排
add = "segments", # 将y = 0的段添加到点
rotate = TRUE, # 垂直旋转图
group = "cyl", # 按cyl进行分组
dot.size = 6, # 设置dot点大小
label = round(dfm$mpg), # 添加mpg值标签
font.label = list(color = "white", size = 9,
vjust = 0.5), # 调整标签参数
ggtheme = theme_pubr() # ggplot2 theme
)
ggdotchart(dfm, x = "name", y = "mpg_z",
color = "cyl", # 按组区分颜色
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # 自定义调色板
sorting = "descending", # 按降序对值进行排序
add = "segments", # 将y = 0的段添加到点
add.params = list(color = "lightgray", size = 2), # 更改元素的颜色和大小
group = "cyl", # 按cyl进行分组
dot.size = 6, # 设置dot点大小
label = round(dfm$mpg_z,1), # 添加mpg值标签
font.label = list(color = "white", size = 9,
vjust = 0.5), # 调整标签参数
ggtheme = theme_pubr() # ggplot2 theme
)+
# 添加水平线
geom_hline(yintercept = 0, linetype = 2, color = "lightgray")
ggdotchart(dfm, x = "name", y = "mpg",
color = "cyl", # 按组区分颜色
palette = c("#00AFBB", "#E7B800", "#FC4E07"), # 自定义调色板
sorting = "descending", # 按降序对值进行排序
rotate = TRUE, # 旋转绘图
dot.size = 2, # 设置dot点大小
y.text.col = TRUE, # 按组上色y轴标签
ggtheme = theme_pubr() # ggplot2 theme
)+
theme_cleveland() # 添加虚线网格
More
Find out more at https://rpkgs.datanovia.com/ggpubr.
Blog posts
- A. Kassambara. ggpubr R Package: ggplot2-Based Publication Ready Plots