R中带有误差线的分组条形图
Grouped barplot in R with error bars
我想绘制一个带有误差条的分组条形图。这是我到目前为止能够得到的那种数字,这对我需要的来说是可以的:
这是我的脚本:
#create dataframe
Gene<-c("Gene1","Gene2","Gene1","Gene2")
count1<-c(12,14,16,34)
count2<-c(4,7,9,23)
count3<-c(36,22,54,12)
count4<-c(12,24,35,23)
Species<-c("A","A","B","B")
df<-data.frame(Gene,count1,count2,count3,count4,Species)
df
mean1<-mean(as.numeric(df[1,][c(2,3,4,5)]))
mean2<-mean(as.numeric(df[2,][c(2,3,4,5)]))
mean3<-mean(as.numeric(df[3,][c(2,3,4,5)]))
mean4<-mean(as.numeric(df[4,][c(2,3,4,5)]))
Gene1SpeciesA.stdev<-sd(as.numeric(df[1,][c(2,3,4,5)]))
Gene2SpeciesA.stdev<-sd(as.numeric(df[2,][c(2,3,4,5)]))
Gene1SpeciesB.stdev<-sd(as.numeric(df[3,][c(2,3,4,5)]))
Gene2SpeciesB.stdev<-sd(as.numeric(df[4,][c(2,3,4,5)]))
ToPlot<-c(mean1,mean2,mean3,mean4)
#plot barplot
plot<-matrix(ToPlot,2,2,byrow=TRUE) #with 2 being replaced by the number of genes!
tplot<-t(plot)
BarPlot <- barplot(tplot, beside=TRUE,ylab="count",
names.arg=c("Gene1","Gene2"),col=c("blue","red"))
#add legend
legend("topright",
legend = c("SpeciesA","SpeciesB"),
fill = c("blue","red"))
#add error bars
ee<-matrix(c(Gene1SpeciesA.stdev,Gene2SpeciesA.stdev,Gene1SpeciesB.stdev,Gene2SpeciesB.stdev),2,2,byrow=TRUE)*1.96/sqrt(4)
tee<-t(ee)
error.bar(BarPlot,tplot,tee)
问题是我需要对 50 个基因和 4 个物种执行此操作,所以我的脚本会变得超级超级长,我想这没有优化...我试图寻求帮助 here but I can't figure out a better way to do what I'd like. If I did not need error bars I could adapt this script 但棘手的部分是混合 ggplot 漂亮的条形图和误差线! ;)
如果您有什么想法可以优化我的脚本,我将不胜感激! :)
非常感谢!
从您对 df
的定义开始,您可以在几行中完成此操作:
library(ggplot2)
cols = c(2,3,4,5)
df1 = transform(df, mean=rowMeans(df[cols]), sd=apply(df[cols],1, sd))
# df1 looks like this
# Gene count1 count2 count3 count4 Species mean sd
#1 Gene1 12 4 36 12 A 16.00 13.856406
#2 Gene2 14 7 22 24 A 16.75 7.804913
#3 Gene1 16 9 54 35 B 28.50 20.240224
#4 Gene2 34 23 12 23 B 23.00 8.981462
ggplot(df1, aes(x=as.factor(Gene), y=mean, fill=Species)) +
geom_bar(position=position_dodge(), stat="identity", colour='black') +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.2,position=position_dodge(.9))
我想绘制一个带有误差条的分组条形图。这是我到目前为止能够得到的那种数字,这对我需要的来说是可以的:
这是我的脚本:
#create dataframe
Gene<-c("Gene1","Gene2","Gene1","Gene2")
count1<-c(12,14,16,34)
count2<-c(4,7,9,23)
count3<-c(36,22,54,12)
count4<-c(12,24,35,23)
Species<-c("A","A","B","B")
df<-data.frame(Gene,count1,count2,count3,count4,Species)
df
mean1<-mean(as.numeric(df[1,][c(2,3,4,5)]))
mean2<-mean(as.numeric(df[2,][c(2,3,4,5)]))
mean3<-mean(as.numeric(df[3,][c(2,3,4,5)]))
mean4<-mean(as.numeric(df[4,][c(2,3,4,5)]))
Gene1SpeciesA.stdev<-sd(as.numeric(df[1,][c(2,3,4,5)]))
Gene2SpeciesA.stdev<-sd(as.numeric(df[2,][c(2,3,4,5)]))
Gene1SpeciesB.stdev<-sd(as.numeric(df[3,][c(2,3,4,5)]))
Gene2SpeciesB.stdev<-sd(as.numeric(df[4,][c(2,3,4,5)]))
ToPlot<-c(mean1,mean2,mean3,mean4)
#plot barplot
plot<-matrix(ToPlot,2,2,byrow=TRUE) #with 2 being replaced by the number of genes!
tplot<-t(plot)
BarPlot <- barplot(tplot, beside=TRUE,ylab="count",
names.arg=c("Gene1","Gene2"),col=c("blue","red"))
#add legend
legend("topright",
legend = c("SpeciesA","SpeciesB"),
fill = c("blue","red"))
#add error bars
ee<-matrix(c(Gene1SpeciesA.stdev,Gene2SpeciesA.stdev,Gene1SpeciesB.stdev,Gene2SpeciesB.stdev),2,2,byrow=TRUE)*1.96/sqrt(4)
tee<-t(ee)
error.bar(BarPlot,tplot,tee)
问题是我需要对 50 个基因和 4 个物种执行此操作,所以我的脚本会变得超级超级长,我想这没有优化...我试图寻求帮助 here but I can't figure out a better way to do what I'd like. If I did not need error bars I could adapt this script 但棘手的部分是混合 ggplot 漂亮的条形图和误差线! ;)
如果您有什么想法可以优化我的脚本,我将不胜感激! :)
非常感谢!
从您对 df
的定义开始,您可以在几行中完成此操作:
library(ggplot2)
cols = c(2,3,4,5)
df1 = transform(df, mean=rowMeans(df[cols]), sd=apply(df[cols],1, sd))
# df1 looks like this
# Gene count1 count2 count3 count4 Species mean sd
#1 Gene1 12 4 36 12 A 16.00 13.856406
#2 Gene2 14 7 22 24 A 16.75 7.804913
#3 Gene1 16 9 54 35 B 28.50 20.240224
#4 Gene2 34 23 12 23 B 23.00 8.981462
ggplot(df1, aes(x=as.factor(Gene), y=mean, fill=Species)) +
geom_bar(position=position_dodge(), stat="identity", colour='black') +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.2,position=position_dodge(.9))