6

Is it possible to add a y-axis to a facet wrap, but only for the first row, as shown in the screenshot?

Code for my plot:

library(ggplot2)

mydf <- read.csv('https://dl.dropboxusercontent.com/s/j3s5sov98q9yvcv/BPdf_by_NB')

ggplot(data = mydf) +
  geom_line(aes(x = YEARMONTH, y = NEWCONS, group = 1), color="darkseagreen3")  +
  geom_line(aes(x = YEARMONTH, y = DEMOLITIONS, group = 1), color = "black") +
  theme_minimal() + 
  labs(title="New constructions Vs Demolitions (2010 - 2014)\n") +
  theme( axis.line = element_blank(),
         axis.title.x = element_blank(),
         axis.title.y = element_blank(),
         axis.text.x = element_blank(),
         axis.text.y = element_blank()) +
  facet_wrap(~ NB) 

Result:

ggplot2 facet wrap

(I've manually added a legend for the place where I want to place the scale)

HAVB
  • 1,858
  • 1
  • 22
  • 37

1 Answers1

7

The idea for this was taken from this answer.

p <- ggplot(data = mydf) +
    geom_line(aes(x = YEARMONTH, y = NEWCONS, group = 1), color="darkseagreen3")  +
    geom_line(aes(x = YEARMONTH, y = DEMOLITIONS, group = 1), color = "black") +
    theme_minimal() + 
    labs(title="New constructions Vs Demolitions (2010 - 2014)\n") +
    theme( axis.line = element_blank(),
                 axis.title.x = element_blank(),
                 axis.text.x = element_blank()) +
    facet_wrap(~ NB) 

Note the changes in the theme call, so that we can selective remove some grobs later.

library(gtable)
p_tab <- ggplotGrob(p)
print(p_tab)

So we want to remove all but four of the left axis items. There's a gtable_filter function using regexes, but it was simpler to just write my own function that does simple negative subsetting (since I couldn't craft the correct regex):

gtable_filter_remove <- function (x, name, trim = TRUE){
    matches <- !(x$layout$name %in% name)
    x$layout <- x$layout[matches, , drop = FALSE]
    x$grobs <- x$grobs[matches]
    if (trim) 
        x <- gtable_trim(x)
    x
}

p_filtered <- gtable_filter_remove(p_tab,name = paste0("axis_l-",5:16),trim=FALSE)

library(grid)
grid.newpage()
grid.draw(p_filtered)

enter image description here

Community
  • 1
  • 1
joran
  • 169,992
  • 32
  • 429
  • 468
  • Almost there! Now there's a y-axis label ("NEWCONS") which I don't need. Just in case, is it possible to avoid the label while keeping the scale? – HAVB Apr 21 '16 at 21:42
  • @HAVB Oh, I think I was just too aggressive about removing that from your original `theme` call. I think if you just put it back in, it'll be fine. – joran Apr 21 '16 at 21:43
  • @HAVB ...by which I mean, put `axis.title.y = element_blank()` back in. Or just `y = NULL` in `labs()`. – joran Apr 21 '16 at 21:47
  • You are right, just needed to put `axis.title.y = element_blank()` back into the theme() call. Answer accepted. – HAVB Apr 21 '16 at 21:49
  • 1
    Something changed in `ggplot2`. I had to use `p_filtered <- gtable_filter_remove(p_tab, name = paste0("axis-l-", 2:4, "-1"), trim = FALSE)` to make it work great again :). Thanks! – Tung Aug 26 '18 at 17:29
  • What's the equivalent to `paste0("axis-l-", 2:4, "-1")` for the x axis? – aclong Jan 27 '21 at 12:11