下面将介绍下如何轻松绘制带有误差线的均值或中位数(如下图)。
加载数据
library(ggpubr)
# ToothGrowth数据集
data("ToothGrowth")
head(ToothGrowth)
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
# mtcars数据集
data("mtcars")
head(mtcars[, c("wt", "mpg", "cyl")])
## wt mpg cyl
## Mazda RX4 2.62 21.0 6
## Mazda RX4 Wag 2.88 21.0 6
## Datsun 710 2.32 22.8 4
## Hornet 4 Drive 3.21 21.4 6
## Hornet Sportabout 3.44 18.7 8
## Valiant 3.46 18.1 6
误差图
要使用的R函数为ggerrorplot()
,其一般格式为:
ggerrorplot(data, x, y, desc_stat = "mean_se")
- data: 数据框
- x, y: 要绘图的x, y变量
- desc_stat: 描述性统计信息,用于绘制误差图,默认值为“mean_se”。可选值为“mean”, “mean_se”, “mean_sd”, “mean_ci”, “mean_range”, “median”, “median_iqr”, “median_mad”, “median_range”
绘制误差图:
# 平均标准差
ggerrorplot(ToothGrowth, x = "dose", y = "len",
desc_stat = "mean_sd")
# 更改误差图类型并添加均值
ggerrorplot(ToothGrowth, x = "dose", y = "len",
desc_stat = "mean_sd",
error.plot = "errorbar", # Change error plot type
add = "mean" # Add mean points
)
还可以添加抖动点,点图和小提琴图:
# 添加抖动点
ggerrorplot(ToothGrowth, x = "dose", y = "len",
desc_stat = "mean_sd", color = "black",
add = "jitter", add.params = list(color = "darkgray")
)
# 添加点图
ggerrorplot(ToothGrowth, x = "dose", y = "len",
desc_stat = "mean_sd", color = "black",
add = "dotplot", add.params = list(color = "darkgray")
)
# 添加小提琴图
ggerrorplot(ToothGrowth, x = "dose", y = "len",
desc_stat = "mean_sd", color = "black",
add = "violin", add.params = list(color = "darkgray")
)
添加P值:
# 指定比较的列表
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
ggerrorplot(ToothGrowth, x = "dose", y = "len",
desc_stat = "mean_sd", color = "black",
add = "violin", add.params = list(color = "darkgray"))+
stat_compare_means(comparisons = my_comparisons)+ # 添加两组间比较的P值
stat_compare_means(label.y = 50) # 添加全局P值
根据分组变量进行着色:
# 根据"dose"变量着色
ggerrorplot(ToothGrowth, x = "dose", y = "len",
desc_stat = "mean_sd",
color = "dose", palette = "jco")
# 根据"supp"变量着色
ggerrorplot(ToothGrowth, x = "dose", y = "len",
desc_stat = "mean_sd",
color = "supp", palette = "jco",
position = position_dodge(0.3) # 调整图形之间的间距
)
折线图
可以使用ggline()
函数来绘制带有误差线的折线图:
# 带有抖动点和平均误差线的折线图
ggline(ToothGrowth, x = "dose", y = "len",
add = c("mean_se", "jitter"))
按分组变量着色:
ggline(ToothGrowth, x = "dose", y = "len",
add = c("mean_se", "jitter"),
color = "supp", palette = "jco")
条形图
可以使用ggbarplot()
函数来绘制条形图:
ggbarplot(ToothGrowth, x = "dose", y = "len",
add = c("mean_se", "jitter"))
按分组变量着色:
ggbarplot(ToothGrowth, x = "dose", y = "len",
add = c("mean_se", "jitter"),
color = "supp", palette = "jco",
position = position_dodge(0.8))
添加标签
数据集:mtcars
# 使用行名作为名称标签
df <- as.data.frame(mtcars[, c("am", "hp")])
df$name <- rownames(df)
head(df)
## am hp name
## Mazda RX4 1 110 Mazda RX4
## Mazda RX4 Wag 1 110 Mazda RX4 Wag
## Datsun 710 1 93 Datsun 710
## Hornet 4 Drive 0 110 Hornet 4 Drive
## Hornet Sportabout 0 175 Hornet Sportabout
## Valiant 0 105 Valiant
绘制带标签的条形图:
set.seed(123)
ggbarplot(df, x = "am", y = "hp",
add = c("mean_se", "point"),
color = "am", fill = "am", alpha = 0.5,
palette = "jco")+
ggrepel::geom_text_repel(aes(label = name))