2

enter image description here

I have a plot like this, made from:

plot = ggplot(dat, aes(x=Nest)) + geom_bar(aes(fill=MotherID), position = position_stack(reverse = TRUE)) + facet_wrap(~Year) + scale_fill_grey(start = 0, end = .9) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + theme(legend.position="none") + ylab("Number of offpsring")

I'd like all the bars to be next to each other without the gaps between them at like 3, 4, 9 on the x axis. I guess it is because nest is a number rather than a string.

I have dat$nest = as.factor(dat$nest)

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
user438383
  • 5,716
  • 8
  • 28
  • 43
  • 1
    Assuming you turned `nest` into a factor before plotting, does `facet_wrap(~ Year, scales="free_x", space="free_x")` resolve your issue? – eipi10 Mar 14 '18 at 20:36
  • yes, thanks, but I only needed the scales="free_x" bit :) – user438383 Mar 14 '18 at 20:39
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 14 '18 at 20:45

1 Answers1

5

If you use facets, the axes are shared between them. However the scales argument can set them free.

facet_wrap(~Year, scales = "free_x")

http://ggplot2.tidyverse.org/reference/facet_wrap.html

Jack Brookes
  • 3,720
  • 2
  • 11
  • 22