如何将彼此相邻的条形图组合在一起?

时间:2014-10-22 17:47:39

标签: r bar-chart

我有以下代码,它绘制了8个平均值之间相等间距的平均值。我需要让酒吧成对组合在一起。我读了很多关于类似问题的答案,但我无法弄清楚我的代码是如何不同的 - 为什么不将它们成对分组?

x = as.numeric(c(filt_forr[1,1:4],filt_forr[1,5:8]))

opar <- par(lwd = 0.3)

plot = barplot(x,axes=FALSE, axisnames=FALSE,ylim=c(0,6),main="Radial Sway of Low and High Frequencies",xlab="Condition",beside=T,ylab="Radial Sway (mm)", cex.names=0.8, las=2, width = 2, col=c("#79c36a","deepskyblue2"),legend = c("Low Frequencies (<0.3 Hz)","Blue = High Frequencies (>0.3 Hz)"))

axis(1,labels=c("Closed/Silence","Closed/Silence","Closed/Noise", "Closed/Noise", "Open/Silence", "Open/Silence","Open/Noise","Open/Noise"),at=plot)

axis(2,at=seq(0,6, by=0.5))

sd = as.numeric(c(filt_forr[2,1:4],filt_forr[2,5:8]))

segments(plot,x-sd,plot,x+sd,lwd=2)

segments(plot-0.1,x-sd,plot+0.1,x-sd,lwd=2)

segments(plot-0.1,x+sd,plot+0.1,x+sd,lwd=2)

abline(c(0,0))

abline(v=0)

以下是我的数据外观示例(第一行有条件,第二行有标准偏差,我用于错误条):

Cond1Low Cond2Low Cond3Low Cond4Low Cond1High Cond2High Cond3High Cond4High

3.503900 2.714572 2.688209 2.739038 2.499589 2.159015 2.1308462 1.9066430

2.073565 1.680001 1.490732 1.259822 1.139851 0.732513 0.8940674 0.6708717

1 个答案:

答案 0 :(得分:1)

要获得并排的条形图,您通常会传递一个值矩阵而不是一个向量,就像您在示例中所做的那样。只需将所有内容更改为相应的矩阵

即可
X <- matrix(x, ncol=2)
plot <- barplot(t(X), beside=T, axes=FALSE, axisnames=FALSE, ylim=c(0,6), 
    main="Radial Sway of Low and High Frequencies",
    xlab="Condition", ylab="Radial Sway (mm)", 
    cex.names=0.8, las=2, width = 2, col=c("#79c36a","deepskyblue2"),
    legend = c("Low Frequencies (<0.3 Hz)","Blue = High Frequencies (>0.3 Hz)"))
axis(1,labels=c("Closed/Silence","Closed/Noise", 
    "Open/Silence","Open/Noise"),at=apply(plot,2,mean))
axis(2,at=seq(0,6, by=0.5))

SD <- matrix(as.numeric(c(filt_forr[2,1:4],filt_forr[2,5:8])), ncol=2)

segments(plot,t(X-SD),plot,t(X+SD),lwd=2)
segments(plot-0.1,t(X-SD),plot+0.1,t(X-SD),lwd=2)
segments(plot-0.1,t(X+SD),plot+0.1,t(X+SD),lwd=2)

abline(c(0,0))
abline(v=0)

enter image description here