我有一个包含四个类别百分比的数据集:前两个类别是“正”,后两个类别是“负”,所以我想对齐 2 和 3 之间的边界,因此它是所有条形上的零点。 (我正在绘制成对的条形图:ext.obs=0 的条形集和 ext.obs=1 的一个条形。)这是数据的一部分:
structure(list(ext.obs = c(0, 0, 0, 1, 1, 1), comp = c(1, 2,
3, 1, 2, 3), `1` = c(0.00617283950617284, 0.00609756097560976,
0.0111111111111111, 0, 0, 0), `2` = c(0.154320987654321, 0.195121951219512,
0.161111111111111, 0.211180124223602, 0.392638036809816, 0.23030303030303
), `3` = c(0.709876543209877, 0.676829268292683, 0.666666666666667,
0.745341614906832, 0.521472392638037, 0.721212121212121), `4` = c(0.12962962962963,
0.121951219512195, 0.161111111111111, 0.0434782608695652, 0.0858895705521472,
0.0484848484848485)), .Names = c("ext.obs", "comp", "1", "2",
"3", "4"), row.names = c(1L, 2L, 3L, 11L, 12L, 13L), class = "data.frame")
我希望能够将我可以做的这些数据的矩阵放在一起
barplot(datamatrix)
让它出来很好。但除了绘制前两个类别然后使用 barplot(..., add=T)
添加后两个类别之外,我想不出其他任何方法。 .这是我写的代码(我实际上用
par(mfrow=c(1, 10)
绘制了 10 对条形)虽然 for(i in 1:10)
循环):bar.loc <- barplot(t(as.matrix(tab3[c(i, i+10), c(5,6)])),
ylim=c(-0.5, 1.0),
col=my.pal[3:4],
xaxt="n",
yaxt="n",
ylab="",
xlab=components[i]
)
barplot(t(as.matrix(tab3[c(i, i+10), c(4, 3)]*(-1))),
add=T,
col=my.pal[2:1],
yaxt="n",
xaxt="n",
ylab="",
xlab="")
可以看到部分成品here或图片如下:
谁能想到一个更优雅的方法来做到这一点?
最佳答案
尝试这个:
barplot( t(cbind(tab3[,5:6],-tab3[,6:5],-tab3[,4:3])),
col=c('lightblue','darkblue',NA,NA,'tan','brown') )
关于r - 居中条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8621039/