如何操纵地块之间的边距?

时间:2015-03-18 19:19:18

标签: r plot

我有三组,每组包含一对条形图。我想在每个组之间添加空格,因此有一些区别。但是,当我通过mar中的par()参数操作边距时,它会影响条形图的宽度。

par(mfrow=c(1,6))
#pair1 
par(mar=c(5,2,5,1), xpd=TRUE)
barplot(t(cbind(1, 5, 6)), col=c("red", "orange", "yellow"))
par(mar=c(5,2,5,2), xpd=TRUE)
barplot(t(cbind(3, 3, 2)), col=c("blue", "green", "purple"))

#pair2
par(mar=c(5,4,5,1), xpd=TRUE)
barplot(t(cbind(2, 2.5, 5)), col=c("red", "orange", "yellow"))
par(mar=c(5,2,5,2), xpd=TRUE)
barplot(t(cbind(5, 1, 3)), col=c("blue", "green", "purple"))

#pair2
par(mar=c(5,4,5,1), xpd=TRUE)
barplot(t(cbind(4, 2, 1)), col=c("red", "orange", "yellow"))
par(mar=c(5,2,5,2), xpd=TRUE)
barplot(t(cbind(6, 2, 1)), col=c("blue", "green", "purple"))

如何在3个地块之间添加更多空间,同时保持条形图的宽度?感谢任何建议。

2 个答案:

答案 0 :(得分:3)

我建议使用layout函数代替par(mfrow=c(1,6)),并指定3对之间的空格作为额外的"空白"绘制区域。

这是一个简单的例子:

tmpmat <- rbind(c(1,2,0,3,4,0,5,6))
layout(tmpmat, widths=c(3,3,1,3,3,1,3,3))

barplot(rbind(1,5,6))
barplot(rbind(3,3,2))

barplot(rbind(2,2.5,5))
barplot(rbind(5,1,3))

barplot(rbind(4,2,1))
barplot(rbind(6,2,1))

另一种可能性是将所有向量组合成一个矩阵并创建1个条形图,然后使用space参数来控制堆积条形之间的空格(我相信这就是@ PedroBraz的回答)这意味着将所有的条形放在同一个垂直刻度上,而你和我的例子给出了每个条形图自己的垂直刻度。

答案 1 :(得分:1)

有一个space参数。

https://stat.ethz.ch/R-manual/R-patched/library/graphics/html/barplot.html

描述:

the amount of space (as a fraction of the average bar width) left before each bar. May be given as a single number or one number per bar. If height is a matrix and beside is TRUE, space may be specified by two numbers, where the first is the space between bars in the same group, and the second the space between the groups. If not given explicitly, it defaults to c(0,1) if height is a matrix and beside is TRUE, and to 0.2 otherwise.