I have the following code, which plots 8 means with equal spacing between the bars. I need to get the bars to group together in pairs. I read a lot of answers to similar questions, but I am having trouble figuring out how my code is different-- why isn't it grouping the bars together in pairs??
我有以下代码,它们绘制了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)
Here is a sample of what my data look like (row one has condition means and row 2 has standard deviations, which I use for the error bars):
以下是我的数据的样本示例(第一行有条件,第二行有标准偏差,我用于错误条):
Cond1Low Cond2Low Cond3Low Cond4Low Cond1High Cond2High Cond3High Cond4High
Cond1Low Cond2Low Cond3Low Cond4Low Cond1High Cond2High Cond3High Cond4High
3.503900 2.714572 2.688209 2.739038 2.499589 2.159015 2.1308462 1.9066430
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
2.073565 1.680001 1.490732 1.259822 1.139851 0.732513 0.8940674 0.6708717
1 个解决方案
#1
1
To get side-by-side barplots you generally pass in a matrix of values rather than a vector as you';ve done in your example. Just change everything to the appropriate matrix
为了得到并排的条形图,你通常会传递一个值矩阵而不是一个向量,就像你在你的例子中所做的那样。只需将所有内容更改为相应的矩阵
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)
#1
1
To get side-by-side barplots you generally pass in a matrix of values rather than a vector as you';ve done in your example. Just change everything to the appropriate matrix
为了得到并排的条形图,你通常会传递一个值矩阵而不是一个向量,就像你在你的例子中所做的那样。只需将所有内容更改为相应的矩阵
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)