Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant bar width in a barplot array in R

Tags:

plot

r

width

I am trying to make a (1 row, 3 column) array of barplots that all have the same bar width.

All three bar plots have a different number of observations and hence the width of the bars end up different for each plot (i.e. plot with the most observations has the narrowest bars and the plot with the fewest observations has the widest bars). I understand from the barplot {graphics} R documentation that,

"Specifying a single value will have no visible effect unless xlim is specified"

However, my x labels are strings, so I'm not sure how to specify xlim. I don't mind if my plots are different widths, but do I need to specify this somehow?

Here is some fake data and the code I am using... thank you for your help.

height4plot1 <- c(1,6,9,2,3,10,7,15)
names4plot1 <- c("P1","P2","P3","P4","P5","P6","P7","P8")

height4plot2 <- c(5,4,10,2)
names4plot2 <- c("M1","M2","M3","M4")

height4plot3 <- c(4,12)
names4plot3 <0 c("U1","U2")

par(mfrow=c(1,3),
    mar=c(10,5,2,1),
    cex.axis=0.7,
    mgp=c(3,0.5,0))
barplot(height4plot1,
    names.arg=names4plot1,
    las=3,
    axes=TRUE,
    axisnames=TRUE,
    ylab="YLAB",
    ylim=ylim,
    plot=TRUE,
    main="PLOT1",
    width=1)
barplot(height4plot2,
    names.arg=names4plot2,
    las=3,
    axes=TRUE,
    axisnames=TRUE,
    ylim=ylim,
    plot=TRUE,
    main="PLOT2",
    width=1)
barplot(height4plot3,
    names.arg=names4plot3,
    las=3,
    axes=TRUE,
    axisnames=TRUE,
    ylim=ylim,
    plot=TRUE,
    main="PLOT3",
    width=1)
like image 771
CJO Avatar asked Nov 04 '22 11:11

CJO


2 Answers

I replaced your irreprodicible example with mine and is seems to work. The xlim argument for barplot is an integer value that ranges from 1 to the number of groups:

tN <- table(Ni <- stats::rpois(100, lambda=5))
opar <- par(mfrow=c(1,3),
    mar=c(10,5,2,1),
    cex.axis=0.7,
    mgp=c(3,0.5,0))
barplot(tN[1:3],
   xlim=c(1, length(tN) ), 
    las=3,
    axes=TRUE,
    axisnames=TRUE,
    ylab="YLAB",
    plot=TRUE,
    main="PLOT1",
    width=1)
barplot(tN[1:4],
   xlim=c(1, length(tN) ), 
    las=3,
    axes=TRUE,
    axisnames=TRUE,
    plot=TRUE,
    main="PLOT2",
    width=1)
barplot(tN[1:5],
   xlim=c(1, length(tN) ), 
    las=3,
    axes=TRUE,
    axisnames=TRUE,
    plot=TRUE,
    main="PLOT3",
    width=1)    
par(opar)
like image 137
IRTFM Avatar answered Nov 09 '22 06:11

IRTFM


Without the variables like height4plot1 etc. it is harder to test your code and modifications, but here are some possibilities:

You can specify width=0.8 for each plot and xlim=c(0,maxnum) where maxnum is the maximum numbers of bars in the different plots. This will leave an empty section in each of the smaller bar plots.

You could pad the shorter vectors of heights with NA until they are all the same length (this would still give the empty sections).

You could concatenate your 3 vectors together and make a single boxplot, possibly coloring the 3 groups differently to help distinquish them. The abline function can be used to add divider lines between the groups (and see the space argument to barplot, or include an 'NA' as divider to give more space between the groups). You can use the return value to help in placing titles above each group.

You can use layout instead of par to set up your 3 plotting areas to have different widths, but getting the exact ratios of widths to give the exact same size bars will not be simple (you may get close with trial and error though). If you really want this route then grconvertX and optim may help.

I would suggest the 2nd option above.

like image 33
Greg Snow Avatar answered Nov 09 '22 06:11

Greg Snow