Constant bar width in barcode array in R

I am trying to create an array of rows (1 row, 3 columns) that have the same column width .

All three bar sections have a different number of observations and, therefore, the width of the bars is ultimately different for each section (that is, the section with the largest number of observations has narrow bands and the section with the least number of observations has the widest bands). I understand from the barplot {graphics} R documentation that

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

However, my x labels are strings, so I'm not sure how to specify xlim . I do not mind if my stories have different widths, but is it necessary to indicate this anyway?

Here are some fake data and the code I use ... thanks 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) 
+4
source share
2 answers

Without variables like height4plot1 , etc. it’s more difficult to check your code and modifications, but here are some possibilities:

You can specify width=0.8 for each chart and xlim=c(0,maxnum) , where maxnum is the maximum number of bars on different charts. This will leave a blank section on each of the small charts.

You can overlap shorter height vectors on the NA until they have the same length (this will still produce empty sections).

You can combine your 3 vectors together and create a single block plotter, perhaps color 3 groups differently to help distinguish them. The abline function can be used to add dividing lines between groups (and see the space argument on barplot or include the NA divider to give more space between groups). You can use the return value to help place headings over each group.

You can use layout instead of par to adjust your 3 print areas to different widths, but getting exact width ratios to get the same size of the bars will not be easy (you can get close to trial and error). If you really want this route, then grconvertX and optim can help.

I would suggest the second option above.

+1
source

I replaced your unreasonable example with mine and it seems to work. The xlim argument to 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) 
+1
source

Source: https://habr.com/ru/post/1445588/


All Articles