Jay
Jay

Reputation: 751

How is the margin between plots manipulated?

I have three groups, each containing a pair of barplots. I would like to add space between each group, so there is some distinction. However, when I manipulate the margins via mar parameter in par(), it affects the width of the barplot.

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"))

How can I add more space in between the 3 plots while maintaining the width of the barplots? Appreciate any advice.

Upvotes: 3

Views: 216

Answers (2)

Greg Snow
Greg Snow

Reputation: 49640

I would suggest using the layout function instead of par(mfrow=c(1,6)) and specify the space between the 3 pairs as additional "blank" plotting regions.

Here is a simple example:

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))

Another possibility is to combine all your vectors into a matrix and create 1 barplot and then use the space argument to control the spaces between the stacked bars (I believe that this is what @PedroBraz's answer was meant to convey. This will put all the bars on the same vertical scale while your and my example gives each barplot its own vertical scale.

Upvotes: 3

Pedro Braz
Pedro Braz

Reputation: 2401

there is a space argument.

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

description:

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.

Upvotes: 1

Related Questions