Indicate the spaces between the strips in the line cabinet

I am trying to create a barplot with R with different widths of bars and different spaces between them. For example, I have a matrix

data <- matrix(c(1,2,2,4,7,1,11,12,3), ncol = 3, byrow = T) colnames(data) <- c("Start", "Stop", "Height") 

And I would like to create such a figure (sorry for the sketch):

 | __ | __ | | | | | ________ | | | | | | | | | ------------------- ------------------ 0 1 2 3 4 5 6 7 8 9 10 11 12 

As far as I understand, barplot () allows you to specify the width, but the space between the columns can only be expressed as part of the average width of the strip. However, I would like to specify specific (integer) numbers for spaces between columns. I will be grateful for any tips / ideas!

+4
source share
2 answers

One way to get what you want is to create dummy empty bars. For instance,

 ##h specifies the heights ##Dummy bars have zero heights h = c(0, 2, 0, 1, 0, 3) w = c(1, 1, 2, 3, 4, 1) 

Then plot the chart using barplot

 ##For the dummy bars, remove the border ##Also set the space=0 to get the correct axis barplot(h, width=w, border=c(NA, "black"), space=0) axis(1, 0:14) 

enter image description here

+3
source

If you split the space argument by mean(Width) , you can also get there:

 data <- as.data.frame(data) data$Width <- with(data, Stop - Start) data$Space <- with(data, Start - c(0,head(Stop,-1))) with(data, barplot(Height, Width, space=Space/mean(Width), xlim=c(0,13) ) ) axis(1,0:14) 

enter image description here

+1
source

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


All Articles