4

I've been able to add vertical space between all facets (Alter just horizontal spacing between facets (ggplot2)) but haven't been able to add just one space between specified facets?

Here's an example based on my real data (in the real plot I have stacked bars):

mydf<-data.frame(year = rep(c(2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017),times = 2),
             Area = rep(c('here','there'),times = 12),
             yearArea = rep(c('here.2016','here.2017', 'there.2016','there.2017'), times = 12),
             treatment = rep(c('control','control','control','treat', 'treat','treat'), times = 4),
             response = rep(c('a','b','c','d'), times = 6),
             count = rep(c(23,15,30,20), times = 6))
mycolour<-c("#999999", "#0072B2", "#009E73","#000000")

Returns plot:

#default facet spacing 
example<-ggplot(data=mydf, aes(x=treatment, y=count, fill=response)) + 
  geom_bar(stat="identity", width = 0.5) +  
  scale_fill_manual(values = mycolour, name = "Response") + 
  labs (y = "Count") +
  facet_grid(~yearArea) + 
  theme_bw()
example

#spacing between each facet
spacedex<-example + theme(panel.spacing.x=unit(2, "lines"))

spacedex

How can I limit the addition of space to only between the second and third facet? (between here.2017 and there.2016)

Emily
  • 470
  • 5
  • 16

1 Answers1

4
library(grid)
gt = ggplot_gtable(ggplot_build(example))
gt$widths[7] = 4*gt$widths[7]
grid.draw(gt)

enter image description here

dww
  • 30,425
  • 5
  • 68
  • 111
  • 1
    thanks for the answer but can you explain briefly what is going on in this code? I'm unable to replicate the same using the `mtcars` dataset. – mnm Mar 06 '18 at 05:38
  • I can add some more information tomorrow. Need to sleep now. Basically, you need to find out which of the widths in gt$widths corresponds to the one you want to change (in this case it is number 7). In the mean time, read up about grid and grid layouts, and you'll probably figure it out before I can get back to this. – dww Mar 06 '18 at 05:44
  • @Ashish, if you play with the two numbers, [7] is the position of the spacing and 4* is the width of the spacing – Emily Mar 06 '18 at 06:31
  • @dww, based on the suggestions, I figured another elegant solution. To expand both vertical and horizontal spacing in a grid, use `panel.spacing` in `theme()`. To expand ONLY the vertical spacing between columns in a grid, use `panel.spacing.x` and to expand ONLY the horizontal spacing between rows in a grid, use `panel.spacing.y` in `theme()`. Thanks, @Emily for the explanation. – mnm Mar 06 '18 at 09:38
  • Thanks @dww for the answer. I am also trying to figure out how to locate the right element for editing. I tried to find a detailed explanation of the gtable structure, but only managed to find a brief (and incomplete) intro to it at https://cran.r-project.org/web/packages/gridExtra/vignettes/gtable.html, which does not go to the depth I needed. I'll be very grateful if you could share a reference for this. – elarry Oct 25 '18 at 16:16
  • @elarry - have a look at [this answer](https://stackoverflow.com/a/49225527/2761575) which has quite a bit of detail. – dww Oct 25 '18 at 18:11