分享ggpubr 包教你绘制密度图直方图柱状图
今天我们要分享的R包是 ggpubr 包,它是一款基于ggplot2的可视化包,功能非常强大,能够一行命令绘制出符合出版物要求的图形。ggpubr 包可绘制的图形类型非常多,有密度图、直方图、柱状图、饼图、棒棒糖图、Cleveland 点图、箱线图、小提琴图、点带图、点图、散点图、线图、误差棒图……哈哈,有木有很期待接下来这个 ggpubr 包的学习了呢!别急,这个包的内容实在是有点多哈,接下来我们会分3期进行详细的讲解,记得紧跟我们的学习哦~
先简单介绍一下今天要分享的绘图内容,分别有:密度图、直方图、柱状图、饼图、棒棒糖图、Cleveland 点图。
接下来,先安装 ggpubr 包:
# Install from CRAN:install.packages("ggpubr")library(ggpubr)
密度图
#先构建数据集
set.seed(1234)df1 <- data.frame(sex=factor(rep(c("F", "M"), each=200)), weight=c(rnorm(200, 55), rnorm(200, 58)))
head(df1)# sex weight# 1 F 53.79293# 2 F 55.27743# 3 F 56.08444# 4 F 52.65430# 5 F 55.42912# 6 F 55.50606
tail(df1)# sex weight# 395 M 58.52875# 396 M 58.78939# 397 M 58.45710# 398 M 58.53883# 399 M 58.01464# 400 M 57.08351
# 基础样式,添加均值线和地毯线,密度图展示不同性别分组重的分布,X轴为体重,Y轴为自动累计的密度ggdensity(df1, x = "weight", fill = "lightgray", add = "mean", rug = TRUE)
# 根据分组设置线条颜色和填充
ggdensity(df1, x="weight", add = "mean", rug = TRUE, color = "sex", fill = "sex", palette = c("#00AFBB", "#E7B800"))
# 更改自定义颜色ggdensity(df1, x="weight", add = "mean", rug = TRUE, color = "sex", fill = "sex", palette = "npg")
# 限定x轴取值范围ggdensity(df1, x="weight", add = "mean", rug = TRUE, color = "sex", fill = "sex", palette = "npg") + xlim(53,60)
# 设置分组分面,更改边框线类型ggdensity(df1, x = "weight", facet.by = "sex", linetype = "dashed", add = "mean", rug = TRUE, color = "sex", fill = "sex", palette = c("#00AFBB", "#E7B800"))
接下来我们详细解读一下用到的 ggdensity 函数:用法:
ggdensity(data, x, y = "..density..", combine = FALSE, merge = FALSE, color = "black", fill = NA, palette = NULL, size = NULL, linetype = "solid", alpha = 0.5, title = NULL, xlab = NULL, ylab = NULL, facet.by = NULL, panel.labs = NULL, short.panel.labs = TRUE, add = c("none", "mean", "median"), add.params = list(linetype = "dashed"), rug = FALSE, label = NULL, font.label = list(size = 11, color = "black"), label.select = NULL, repel = FALSE, label.rectangle = FALSE, ggtheme = theme_pubr(), ...)
参数:篇幅有限,没有用到的自行探索~,有的参数在后面的其他图形还会讲解
data
所需的数据框 dataframe
x
进行作图所需的数据
y
设置为密度/数量(density/count)
combine
对于多个变量的数据是否分面
merge
对于多个变量的数据是否合并,默认是FALSE
color, fill
线条颜色与填充色
palette
自定义颜色画板
size
设置点和轮廓的大小
linetype
线条类型
alpha
透明度设置
title
设置标题
xlab
设置x轴标题
ylab
设置y轴标题
facet.by
设置分组分面
panel.labs
设置分面各组的标题
short.panel.labs
是否缩写分面标题,逻辑值,默认是TRUE。
add
添加均值线或中位数线,选项有"mean" or "median"
add.params
给add参数的对象添加其他参数/属性
rug
逻辑值,若为TRUE,在X轴上添加地毯线显示样本的分布
label
设置列标签
font.label
设置标签字体
repel
逻辑值,是否使用ggrepel避免字体重叠
label.rectangle
是否给标签添加方框
ggtheme
设置画图主题
直方图
用到的数据还是 df1,直方图只是把密度还原成了原始数据counts值gghistogram(df1, x="weight", add = "mean", rug = TRUE, fill = "lightgray")
# 按照分组设置边框颜色gghistogram(df1, x="weight", add = "mean", rug = TRUE, color ="sex", palette = c("#00AFBB", "#E7B800"))
# 按照分组设置填充颜色
gghistogram(df1, x="weight", add = "mean", rug = TRUE, fill = "sex", palette = c("#00AFBB", "#E7B800"))
# 同时设置边框和填充颜色gghistogram(df1, x="weight", add = "mean", rug = TRUE, color = "sex", fill = "sex", palette = c("#00AFBB", "#E7B800"))