Reputation: 776
I am trying to generate a barplot with R with different widths of the 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 generate a figure like this (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 bars can only be expressed as a fraction of the average bar width. However, I would like to specify specific (integer) numbers for the spaces between the bars. I'll appreciate any hints/ideas!
Upvotes: 7
Views: 3430
Reputation: 93908
If you divide 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)
Upvotes: 3
Reputation: 60492
One way of getting what you want is to create dummy, empty bars. For example,
##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 using a 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)
Upvotes: 4