下面將介紹下如何輕松繪制帶有誤差線的均值或中位數(shù)(如下圖)。
加載數(shù)據(jù)
library(ggpubr)
# ToothGrowth數(shù)據(jù)集
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數(shù)據(jù)集
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函數(shù)為ggerrorplot()
,其一般格式為:
ggerrorplot(data, x, y, desc_stat = "mean_se")
- data: 數(shù)據(jù)框
- x, y: 要繪圖的x, y變量
- desc_stat: 描述性統(tǒng)計信息嵌溢,用于繪制誤差圖逛漫,默認值為“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值
根據(jù)分組變量進行著色:
# 根據(jù)"dose"變量著色
ggerrorplot(ToothGrowth, x = "dose", y = "len",
desc_stat = "mean_sd",
color = "dose", palette = "jco")
# 根據(jù)"supp"變量著色
ggerrorplot(ToothGrowth, x = "dose", y = "len",
desc_stat = "mean_sd",
color = "supp", palette = "jco",
position = position_dodge(0.3) # 調(diào)整圖形之間的間距
)
折線圖
可以使用ggline()
函數(shù)來繪制帶有誤差線的折線圖:
# 帶有抖動點和平均誤差線的折線圖
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()
函數(shù)來繪制條形圖:
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))
添加標簽
數(shù)據(jù)集: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))