本篇介紹如何利用R軟件和ggplot2制作誤差線的條形圖,我們可以使用如下幾個(gè)函數(shù)制作不同類型的誤差線圖形:
- geom_errorbar()
- geme_linerange()
- geom_pointrange()
- geom_crossbar()
- geom_errorbarh()
條形圖和線圖上增加誤差線
準(zhǔn)備數(shù)據(jù)
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 : Tooth length
- dose : Dose in milligrams (0.5, 1, 2)
- supp : Supplement type (VC or OJ)
在下面的例子中,我們將繪制每組Tooth長(zhǎng)度的平均值。標(biāo)準(zhǔn)差用于在圖表上繪制誤差線。首先,下面寫(xiě)個(gè)函數(shù)將用于計(jì)算每組中感興趣變量的均值和標(biāo)準(zhǔn)差:
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)
}
Ok,我們運(yùn)用到我們數(shù)據(jù)里
df2 <- data_summary(ToothGrowth, varname="len",
groupnames=c("supp", "dose"))
# Convert dose to a factor variable
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
帶誤差線的條形圖
使用geom_errorbar()函數(shù)
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'))

圖片.png
若我們只想保留誤差線的上面的部分,只需將geom_errorbar函數(shù)中ymin = len - sd改為 ymin = len

圖片.png
帶誤差線的線圖
# 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'))

圖片.png
我們也可以使用函數(shù)geom_pointrange()或者geom_linerange()代替geom_errorbar()函數(shù)
# 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))

圖片.png
帶有均值和誤差線的點(diǎn)圖
除了條形圖,我們也可以換用另一種形式來(lái)表示。我們可以使用geom_dotplot()和stat_summary()函數(shù)。點(diǎn)圖代替條形圖代表數(shù)據(jù)范圍再加上誤差線是同樣的效果:
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")

圖片.png
Base plot繪制誤差線條形圖
除了使用ggplot2函數(shù)繪制,我們也可以使用最基礎(chǔ)的圖形語(yǔ)法繪制,這里給個(gè)示例,可以了解學(xué)習(xí)一下:
創(chuàng)建數(shù)據(jù)集
#創(chuàng)建示范數(shù)據(jù)集
data <- data.frame(
specie=c(rep("sorgho" , 10) , rep("poacee" , 10) ),
cond_A=rnorm(20,10,4),
cond_B=rnorm(20,8,3),
cond_C=rnorm(20,5,4))
#求均值
bilan <- aggregate(cbind(cond_A,cond_B,cond_C)~specie , data=data , mean)
rownames(bilan) <- bilan[,1]
bilan <- as.matrix(bilan[,-1])
#Plot boundaries
lim <- 1.2*max(bilan)
#求方差
stdev <- aggregate(cbind(cond_A,cond_B,cond_C)~specie , data=data , sd)
rownames(stdev) <- stdev[,1]
stdev <- as.matrix(stdev[,-1]) * 1.96 / 10 #設(shè)置下方差的范圍,防止過(guò)度分散
#構(gòu)造函數(shù)創(chuàng)建errobar
error.bar <- function(x, y, upper, lower=upper, length=0.1,...){
arrows(x,y+upper, x, y-lower, angle=90, code=3, length=length, ...)
}
出圖
ze_barplot <- barplot(bilan , beside=T , legend.text=T,col=c("blue" , "skyblue") , ylim=c(0,lim) , ylab="height")
error.bar(ze_barplot,bilan, stdev)

圖片.png
歡迎大家關(guān)注我的公眾號(hào)【BioparaMeta】,定期分享生信小技巧,一起交流,學(xué)習(xí),進(jìn)步?。?/p>