在基地R中是否有办法手动将第二个条形图添加到现有条形图中。我知道如果两个系列来自同一个数据对象(使用barplot( ... beside=T)
),或者我想可以绘制rect(...)
包裹的矩形(barplot
),该怎么做。如果您的数据来自不同的对象,那么您如何使用barplot
函数执行此操作?如何控制酒吧位置?
我使用space参数尝试了这个(显然不起作用):
h1 <- c(10,5,1)
h2 <- c(8, 3, 1)
barplot(h1, width = 0.5, space = 2, col='red')
barplot(h2, width = 0.5, space = 2.5, col='blue', add=T)
使用beside=T
参数时,除了彼此之外不可能得到这些条。
期望的输出是这样的:
barplot(matrix(c(h1, h2), nrow=2, byrow=T), beside=T, col=c('red', 'blue'))
更新:如何运作
为了让我最终 - 我希望 - 理解宽度和空间参数,我们可以绘制轴并使用蓝色数据的参数。
barplot(h1, width = 0.5, space = 2, col='red')
axis(1, seq(0, 10, 0.5)) #way out of the plot region
barplot(h2, width = 0.25, space = c(4,2,4), col='blue', add=T)
从这看起来如果(如果我错了,请纠正我):
1。 width
是每个条的宽度 - 根据需要进行回收
2. space
控制前一个栏的空间(左侧)或第一个栏的0,并计算为当前栏的宽度*空间 - 再循环有必要的。所以第一个蓝条开始于(空格到0)0.25 * 4 = 1,右边是1 + 0.25 = 1.25;第二个柱子从1.25 + 0.25 * 2 = 1.75开始,右侧是1.75 + 0.25 = 2.等等...