Error bar
本教程介绍了如何使用R软件和ggplot2软件包创建带有误差线的图形。
1. 数据准备
rm(list = ls())
# Add error bars to a bar and line plots
library(ggplot2)
df <- ToothGrowth
df$dose <- factor(df$dose)
head(df)
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
统计函数代码
library(plyr)
# 下面的辅助函数将用于计算平均值和标准偏差
data_summary <- function(data, varname, groupnames){
require(plyr)
summary_func <- function(x, col){
c(mean = mean(x[[col]], na.rm=TRUE),
sd = sd(x[[col]], na.rm=TRUE))
}
data_sum<-ddply(data, groupnames, .fun=summary_func,
varname)
data_sum <- rename(data_sum, c("mean" = varname))
return(data_sum)
}
df2 <- data_summary(ToothGrowth, varname = "len",
groupnames = c("supp", "dose"))
# Convert dose to a factor variable
df2$dose <- factor(df2$dose)
head(df2)
supp dose len sd
1 OJ 0.5 13.23 4.459709
2 OJ 1 22.70 3.910953
3 OJ 2 26.06 2.655058
4 VC 0.5 7.98 2.746634
5 VC 1 16.77 2.515309
6 VC 2 26.14 4.797731
或者使用Rmisc包统计函数
# Rmisc包包含统计函数
library(Rmisc)
df2_count <- summarySE(df, measurevar = "len",
groupvars = c("supp","dose"))
head(df2_count)
supp dose N len sd se ci
1 OJ 0.5 10 13.23 4.459709 1.4102837 3.190283
2 OJ 1 10 22.70 3.910953 1.2367520 2.797727
3 OJ 2 10 26.06 2.655058 0.8396031 1.899314
4 VC 0.5 10 7.98 2.746634 0.8685620 1.964824
5 VC 1 10 16.77 2.515309 0.7954104 1.799343
6 VC 2 10 26.14 4.797731 1.5171757 3.432090
2. 带误差条的条形图
# The function geom_errorbar() can be used to produce the error bars:
library(ggplot2)
# Fefault bar plot
p1 <- ggplot(df2, aes(x = dose, y = len, fill = supp)) +
geom_bar(stat = "identity", color = "black", position = position_dodge()) +
geom_errorbar(aes(ymin = len - sd, ymax = len + sd),
width = 0.2, position = position_dodge(0.9))
# Finished bar plot
p2 <- p1 + labs(title = "Tooth length per dose", x = "Dose(mg)", y = "Length") +
theme_minimal() +
scale_fill_manual(values = c('#999999','#E69F00'))
# Keep only the upper error bars
p3 <- ggplot(df2, aes(x = dose, y = len, fill = supp)) +
geom_bar(stat = "identity", color = "black", position = position_dodge()) +
geom_errorbar(aes(ymin = len, ymax = len + sd),
width = 0.2, position = position_dodge(0.9))
library(ggpubr)
ggarrange(p1, p2,p3, labels = c("A","B","C"),
nrow = 1)
3. 带误差条的线图
# Default line plot
p4 <- ggplot(df2, aes(x= dose, y = len, color = supp, group = supp)) +
geom_line(position = position_dodge(0.1)) +
geom_point(position = position_dodge(0.1)) +
geom_errorbar(aes(ymin = len - sd, ymax = len + sd),
width = 0.2, position = position_dodge(0.1) )
# Finished line plot
p5 <- p4 + labs(title="Tooth length per dose", x="Dose (mg)", y = "Length") +
theme_minimal() +
scale_color_manual(values=c('#999999','#E69F00'))
# Use geom_pointrange
p6 <- ggplot(df2, aes(x = dose, y = len, group = supp, color = supp)) +
geom_pointrange(aes(ymin = len - sd, ymax = len + sd))
# Use geom_pointrange() + geom_line()
p7 <- ggplot(df2, aes(x = dose, y = len, group = supp, color = supp)) +
geom_line(position = position_dodge(0.1)) +
geom_pointrange(aes(ymin = len - sd, ymax = len + sd), position = position_dodge(0.1))
library(ggpubr)
ggarrange(p4,p5,p6,p7, labels = c("A","B","C","D"),
nrow = 2, ncol = 2)
4. 带有均值点和误差线的点图
# Dot plot with mean point and error bars
library(Hmisc)
p8 <- ggplot(df, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center')
# Use geom_crossbar()
p9 <- p8 + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="crossbar", width=0.5)
# Use geom_errorbar()
p10 <- p8 + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="errorbar", color="red", width=0.2) +
stat_summary(fun.y=mean, geom="point", color="red")
# Use geom_pointrange()
p11 <- p8 + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="pointrange", color="red")
library(ggpubr)
ggarrange(p8,p9,p10,p11, labels = c("A","B","C","D"),
nrow = 2, ncol = 2)