14

geom_bar seems to work best when it has fixed width bars - even the spaces between bars seem to be determined by width, according to the documentation. When you have variable widths, however, it does not respond as I would expect, leading to overlaps or gaps between the different bars (as shown here).

To see what I mean, please try this very simple reproducible example:

x <- c("a","b","c")
w <- c(1.2, 1.3, 4) # variable widths
y <- c(9, 10, 6) # variable heights

ggplot() + 
geom_bar(aes(x = x, y = y, width = w, fill=x), 
 stat="identity", position= "stack")

What I really want is for the different bars to be just touching, but not overlapping, like in a histogram.

I've tried adding position= "stack", "dodge", and "fill, but none work. Does the solution lie in geom_histogram or am I just not using geom_bar correctly?

geom-plot overlap

P.s. to see the issue with gaps, try replacing 4 with 0.5 in the above code and see the outcome.

Community
  • 1
  • 1
RobinLovelace
  • 4,799
  • 6
  • 29
  • 40

2 Answers2

18

Seems that there isn't any straightforward solution, so we should treat x-axis as continuous in terms of w and manually compute required positions for ticks and bar centers (this is useful):

# pos is an explicit formula for bar centers that we are interested in:
#        last + half(previous_width) + half(current_width)
pos <- 0.5 * (cumsum(w) + cumsum(c(0, w[-length(w)])))
ggplot() + 
  geom_bar(aes(x = pos, width = w, y = y, fill = x), stat = "identity") + 
  scale_x_continuous(labels = x, breaks = pos)

enter image description here

Community
  • 1
  • 1
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
  • That solves the problem, many thanks. Perhaps a functionality for ggplot2 devs to consider adding as a position option? – RobinLovelace Dec 19 '13 at 23:57
  • @RobinLovelace, no problem. Maybe, you could try to [ask](https://github.com/hadley/ggplot2/issues) for one. – Julius Vainora Dec 20 '13 at 00:04
  • Thanks for the advice. I did as for this https://github.com/hadley/ggplot2/issues/890 but can't see this on the ggplot issues page. Seems to have been removed - any ideas? – RobinLovelace Dec 23 '13 at 13:29
  • @RobinLovelace, seems fine to me. I believe it will be moved to "feature" in case they will think this feature should be added. – Julius Vainora Dec 23 '13 at 13:40
  • Wouldn't it be easier to use `scale_x_discrete` for something like this? – NiuBiBang Dec 26 '13 at 23:01
  • @DaNiu, as far as I know none of the parameters of `scale_x_discrete` allows do to that. There is no way to measure the x-axis when data is discrete. Feel free to share your approach with us if you are aware of one. – Julius Vainora Dec 26 '13 at 23:10
  • @RobinLovelace, I looked at adding this to ggplot2 as well but it wasn't obvious to me that it would fit into neatly e.g. as a geom. So I started with the answer above and wrote a function, then a package- see the answer I just added to this question – Eric Jan 03 '18 at 22:06
  • As a matter of fact, I don't think you need `0.5 * (...)` – tjebo Feb 15 '21 at 08:17
4

You can now do this with the mekko package: https://cran.r-project.org/web/packages/mekko/vignettes/mekko-vignette.html

Eric
  • 177
  • 1
  • 7