是否可以将y轴添加到构面包装中,但仅限于第一行,如屏幕截图所示?
我的情节代码:
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)
结果:
(我手动添加了一个想要放置比例的地方的图例)
答案 0 :(得分:5)
这个想法取自this回答。
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)
请注意theme
调用中的更改,以便我们以后可以选择性地删除一些grobs。
library(gtable)
p_tab <- ggplotGrob(p)
print(p_tab)
所以我们要删除除左边四个项目之外的所有项目。有一个使用正则表达式的gtable_filter
函数,但只编写我自己的函数做简单的负数子集(因为我无法制作正确的正则表达式)更简单:
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)