r - ggplot2 facet wrap: y-axis scale on the first row only -
is possible add y-axis facet wrap, first row, shown in screenshot?
code 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:
(i've manually added legend place want place scale)
the idea taken 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 changes in theme
call, can selective remove grobs later.
library(gtable) p_tab <- ggplotgrob(p) print(p_tab)
so want remove 4 of left axis items. there's gtable_filter
function using regexes, simpler write own function simple negative subsetting (since couldn't craft 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)
Comments
Post a Comment