ggplot2小平面换行: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)
结果:
(我已经为要放置秤的位置手动添加了图例)
此想法取自此答案
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
调用中的更改,以便我们以后可以有选择地删除一些杂物.
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)
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:
(I've manually added a legend for the place where I want to place the scale)
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)
这篇关于ggplot2小平面换行:y轴比例仅在第一行上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!