作者:假装坚持-我很不爽_547 | 来源:互联网 | 2022-12-02 18:33
我想在使用facet_wrap()
和刻面使用两个变量并且两个都可以自由缩放时删除带状标签的冗余。
例如,facet_wrap
以下图形的此版本
library(ggplot2)
dt <- txhousing[txhousing$year %in% 2000:2002 & txhousing$month %in% 1:3,]
ggplot(dt, aes(median, sales)) +
geom_point() +
facet_wrap(c("year", "month"),
labeller = "label_both",
scales = "free")
应该具有此facet_grid
版本的外观,其中带状标签位于图形的顶部和右侧(也可以是底部和左侧)。
ggplot(dt, aes(median, sales)) +
geom_point() +
facet_grid(c("year", "month"),
labeller = "label_both",
scales = "free")
不幸的是,使用facet_grid
不是一种选择,因为据我所知,它不允许秤“完全免费” -参见此处或此处
我考虑过的一种尝试是制作单独的地块,然后将它们组合起来:
library(cowplot)
theme_set(theme_gray())
p1 <- ggplot(dt[dt$year == 2000,], aes(median, sales)) +
geom_point() +
facet_wrap("month", scales = "free") +
labs(y = "2000") +
theme(axis.title.x = element_blank())
p2 <- ggplot(dt[dt$year == 2001,], aes(median, sales)) +
geom_point() +
facet_wrap("month", scales = "free") +
labs(y = "2001") +
theme(strip.background = element_blank(),
strip.text.x = element_blank(),
axis.title.x = element_blank())
p3 <- ggplot(dt[dt$year == 2002,], aes(median, sales)) +
geom_point() +
facet_wrap("month", scales = "free") +
labs(y = "2002") +
theme(strip.background = element_blank(),
strip.text.x = element_blank())
plot_grid(p1, p2, p3, nrow = 3)
我可以接受上述骇客尝试,但是我想知道是否有什么facet_wrap
可以允许所需的输出。我觉得我错过了一些显而易见的事情,也许我在寻找答案时没有包含正确的关键词(我觉得这个问题之前已经解决过)。