ggplot2-为折线图和条形图添加误差线
采⽤ggplot2绘制折线图和条形图,并添加误差线.
点用英语怎么读ggplot2只能处理 data.frame数据,每列作为⼀个变量,是⼀个指标.
以ToothGrowth数据为例,进⾏处理
tg <- ToothGrowth
head(tg)
四好老师## len supp do
## 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(ggplot2)
数据预处理
# summarySE 计算标准差和标准误差以及95%的置信区间.
tgc <- summarySE(tg, measurevar="len", groupvars=c("supp","do"))
tgc
## supp do N len sd ci
## 1 OJ 0.5 10 13.23 4.459709 1.4102837 3.190283
## 2 OJ 1.0 10 22.70 3.910953 1.2367520 2.797727
## 3 OJ 2.0 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.0 10 16.77 2.515309 0.7954104 1.799343
## 6 VC 2.0 10 26.14 4.797731 1.5171757 3.432090
折线图
绘制带有误差线和95%置信区间线的折线图和点图
# 带有标准误差线的折线图
# Standard error of the mean
ggplot(tgc, aes(x=do, y=len, colour=supp))+
geom_errorbar(aes(ymin=len-, ymax=len+), width=.1)+
geom_line()+
geom_point()
# 对重叠的点,进⾏偏移处理(尽管这样可以将点分开便于观看,但是个⼈认为这并不科学) pd <- position_dodge(0.1)# move them .05 to the left and right
ggplot(tgc, aes(x=do, y=len, colour=supp))+
geom_errorbar(aes(ymin=len-, ymax=len+), width=.1, position=pd)+
geom_line(position=pd)+
geom_point(position=pd)
裹的成语
## ymax not defined: adjusting position using y instead
## ymax not defined: adjusting position using y instead
# 绘制带有95%置信区间的折线图
ggplot(tgc, aes(x=do, y=len, colour=supp))+
geom_errorbar(aes(ymin=len-ci, ymax=len+ci), width=.1, position=pd)+
geom_line(position=pd)+
geom_point(position=pd)
## ymax not defined: adjusting position using y instead
## ymax not defined: adjusting position using y instead
# 设置误差线的颜⾊,特别注意如果没有 group=supp,这个重合的误差线将不会偏移.
ggplot(tgc, aes(x=do, y=len, colour=supp, group=supp))+
geom_errorbar(aes(ymin=len-ci, ymax=len+ci), colour="black", width=.1, position=pd)+
geom_line(position=pd)+
geom_point(position=pd, size=3)
## ymax not defined: adjusting position using y instead
## ymax not defined: adjusting position using y instead
下⾯是⼀个完整的带有标准误差线的图,geom_point 放在 geom_line之后,可以保证点被最后绘制,填充为⽩⾊.
ggplot(tgc, aes(x=do, y=len, colour=supp, group=supp))+
geom_errorbar(aes(ymin=len-, ymax=len+), colour="black", width=.1, position=pd)+
geom_line(position=pd)+
geom_point(position=pd, size=3, shape=21, fill="white")+# 21 is filled circle
xlab("Do (mg)")+
ylab("Tooth length")+
scale_colour_hue(name="Supplement type", # Legend label, u darker colors
breaks=c("OJ", "VC"),
labels=c("Orange juice", "Ascorbic acid"),
l=40)+# U darker colors, lightness=40
ggtitle("The Effect of Vitamin C on\nTooth Growth in Guinea Pigs")+
里安娜
expand_limits(y=0)+# Expand y range
scale_y_continuous(breaks=0:20*4)+# Set tick every 4
theme_bw()+
theme(legend.justification=c(1,0),# 这⼀项很关键,如果没有这个参数,图例会偏移,读者可以试⼀试
legend.position=c(1,0))# Position legend in bottom right
## ymax not defined: adjusting position using y instead
## ymax not defined: adjusting position using y instead
条形图
绘制条形图与绘制折线图类似,但是必须要注意的是tgc$size必须被设置成 factor 类型,如果它是数值型向量,那么将会出现错误. > 这是因为do如果是数值型向量将会作为连续型数据进⾏处理,⽽因⼦型变量被作为离散型数据进⾏处理.
# 转换为因⼦类型
tgc2 <- tgc
小班消防安全教案
tgc2$do <- factor(tgc2$do)
# Error bars reprent standard error of the mean
ggplot(tgc2, aes(x=do, y=len, fill=supp))+
geom_bar(position=position_dodge(), stat="identity")+
geom_errorbar(aes(ymin=len-, ymax=len+),
width=.2, # 设置误差线的宽度
position=position_dodge(.9))
# 使⽤95%置信区间
ggplot(tgc2, aes(x=do, y=len, fill=supp))+
geom_bar(position=position_dodge(), stat="identity")+
geom_errorbar(aes(ymin=len-ci, ymax=len+ci),
width=.2, # Width of the error bars
position=position_dodge(.9))
完整的条形图
ggplot(tgc2, aes(x=do, y=len, fill=supp))+
geom_bar(position=position_dodge(), stat="identity",
雪莲果种植colour="black", # U black outlines,
size=.3)+# Thinner lines
焦虑症是什么原因引起的
geom_errorbar(aes(ymin=len-, ymax=len+),
size=.3, # Thinner lines
width=.2,
position=position_dodge(.9))+
xlab("Do (mg)")+
ylab("Tooth length")+
scale_fill_hue(name="Supplement type", # Legend label, u darker colors breaks=c("OJ", "VC"),
labels=c("Orange juice", "Ascorbic acid"))+
ggtitle("The Effect of Vitamin C on\nTooth Growth in Guinea Pigs")+
scale_y_continuous(breaks=0:20*4)+
theme_bw()