ggplot2 facet wrap:y 轴刻度仅在第一行
ggplot2 facet wrap: y-axis scale on the first row only
是否可以将 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)
结果:
(我在要放置刻度的地方手动添加了图例)
这个想法来自 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
调用中的更改,以便我们稍后可以选择性地删除一些 grob。
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)
是否可以将 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)
结果:
(我在要放置刻度的地方手动添加了图例)
这个想法来自 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
调用中的更改,以便我们稍后可以选择性地删除一些 grob。
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)