給直方圖和線圖添加誤差棒
準(zhǔn)備數(shù)據(jù)
這里使用ToothGrowth 數(shù)據(jù)集。它描述了維他命C對Guinea豬牙齒的生長影響。包含了三種不同的劑量(Vitamin C (0.5, 1, and 2 mg))和相應(yīng)的兩種不同使用方法( [orange juice (OJ) or ascorbic acid (VC)])毛肋。
library(ggplot2)
df <- ToothGrowth
df$dose <- as.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
- len :牙齒長度
- dose : 劑量 (0.5, 1, 2) 單位是毫克
- supp : 支持類型 (VC or OJ)
在下面的例子中谷羞,我們將繪制每組中牙齒長度的均值寞奸。標(biāo)準(zhǔn)差用來繪制圖形中的誤差棒吃溅。
首先,下面的幫助函數(shù)會用來計算每組中興趣變量的均值和標(biāo)準(zhǔn)差:
#+++++++++++++++++++++++++
# 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)
}
統(tǒng)計數(shù)據(jù) :
df2 <- data_summary(ToothGrowth, varname="len",
groupnames=c("supp", "dose"))
# 把劑量轉(zhuǎn)換為因子變量
df2$dose=as.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
有誤差棒的直方圖
函數(shù) geom_errorbar()可以用來生成誤差棒:
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
閱讀ggplot2直方圖更多信息 : ggplot2 bar graphs
有誤差棒的線圖
# 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
你也可以使用函數(shù) geom_pointrange() 或 geom_linerange() 替換 geom_errorbar()
# 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
閱讀ggplot2線圖更多信息: ggplot2 line plots
有均值和誤差棒的點圖
使用函數(shù) geom_dotplot() and stat_summary() :
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
閱讀ggplot2點圖更多信息: ggplot2 dot plot
線程信息
This analysis has been performed using R software (ver. 3.2.4) and ggplot2 (ver. 2.1.0)
僅作翻譯學(xué)習(xí)使用拧篮。