# 给直方图和线图添加误差棒

## 准备数据

``````library(ggplot2)
df <- ToothGrowth
df\$dose <- as.factor(df\$dose)
``````
``````##    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
``````
• len :牙齿长度
• dose : 剂量 (0.5, 1, 2) 单位是毫克
• supp : 支持类型 (VC or OJ)

``````#+++++++++++++++++++++++++
# Function to calculate the mean and the standard deviation
# for each group
#+++++++++++++++++++++++++
# data : a data frame
# varname : the name of a column containing the variable
#to be summariezed
# groupnames : vector of column names to be used as
# grouping variables
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"))
# 把剂量转换为因子变量
df2\$dose=as.factor(df2\$dose)
``````
``````##   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
``````

## 有误差棒的直方图

``````library(ggplot2)
# Default bar plot
p<- 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=.2,
position=position_dodge(.9))
print(p)
# Finished bar plot
p+labs(title="Tooth length per dose", x="Dose (mg)", y = "Length")+
theme_classic() +
scale_fill_manual(values=c('#999999','#E69F00'))
``````
img
img

``````# Keep only upper error bars
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=.2,
position=position_dodge(.9))
``````
img

## 有误差棒的线图

``````# Default line plot
p<- ggplot(df2, aes(x=dose, y=len, group=supp, color=supp)) +
geom_line() +
geom_point()+
geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.2,
position=position_dodge(0.05))
print(p)
# Finished line plot
p+labs(title="Tooth length per dose", x="Dose (mg)", y = "Length")+
theme_classic() +
scale_color_manual(values=c('#999999','#E69F00'))
``````
img
img

``````# Use geom_pointrange
ggplot(df2, aes(x=dose, y=len, group=supp, color=supp)) +
geom_pointrange(aes(ymin=len-sd, ymax=len+sd))
# Use geom_line()+geom_pointrange()
ggplot(df2, aes(x=dose, y=len, group=supp, color=supp)) +
geom_line()+
geom_pointrange(aes(ymin=len-sd, ymax=len+sd))
``````
img
img

# 有均值和误差棒的点图

The mean +/- SD can be added as a crossbar , a error bar or a pointrange :

``````p <- ggplot(df, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center')
# use geom_crossbar()
p + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="crossbar", width=0.5)
# Use geom_errorbar()
p + 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()
p + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="pointrange", color="red")
``````
img
img
img

# 线程信息

This analysis has been performed using R software (ver. 3.2.4) and ggplot2 (ver. 2.1.0)

### 推荐阅读更多精彩内容

• 简介 文章较长，点击直达我的博客，浏览效果更好。本文内容基本是来源于STHDA，这是一份十分详细的ggplot2使...
taoyan阅读 27,720评论 6 104
• 问题 你想要为一个数据集绘制均值的误差棒。 方案 想要用ggplot2绘制图形，数据必须是数据框形式，而且是长格式...
王诗翔阅读 6,703评论 3 14
• 本文参考 《R 语言绘图速查手册 v0.1beta》 图目录1 基本散点图;200 个正态分布的随机数2 poin...
thinkando阅读 4,733评论 11 90
• 红绸涌动映彩霞， 金钵闪烁幸福袈。 亭台怎勘负壁画， 望眼欲穿是繁华。
康真阅读 35评论 5 3
• 朋友小雅是我们眼中闪闪发光的那类人，说得俗点，就是美貌与智慧并重，身材和情商皆高。如此，身边追求者自然甚众。 ...
左佳妮阅读 24评论 6 1