gpt4 book ai didi

r - 如何像在 facet_grid 中一样在 facet_wrap 中定位 strip 标签

转载 作者:行者123 更新时间:2023-12-04 08:20:21 24 4
gpt4 key购买 nike

我想在使用 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_wrap_version

应该有这个样子 facet_grid它的版本,其中 strip 标签位于图形的顶部和右侧边缘(也可以是底部和左侧边缘)。
ggplot(dt, aes(median, sales)) +
geom_point() +
facet_grid(c("year", "month"),
labeller = "label_both",
scales = "free")

enter image description here

不幸的是,使用 facet_grid不是一种选择,因为据我所知,它不允许秤“完全免费” - 见 herehere

我想到的一种尝试是制作单独的图,然后将它们组合起来:
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)

enter image description here

我对上述骇人听闻的尝试没有意见,但我想知道 facet_wrap 中是否有内容这可以允许所需的输出。 我觉得我错过了一些明显的东西,也许我对答案的搜索没有包含正确的关键词(我觉得这个问题之前已经解决了)。

最佳答案

这似乎并不容易,但一种方法是使用网格图形将 facet_grid 图中的面板条插入到创建为 facet_wrap 的面板中。像这样的东西:

首先让我们使用 facet_grid 和 facet_wrap 创建两个图。

dt <- txhousing[txhousing$year %in% 2000:2002 & txhousing$month %in% 1:3,]

g1 = ggplot(dt, aes(median, sales)) +
geom_point() +
facet_wrap(c("year", "month"), scales = "free") +
theme(strip.background = element_blank(),
strip.text = element_blank())

g2 = ggplot(dt, aes(median, sales)) +
geom_point() +
facet_grid(c("year", "month"), scales = "free")

现在我们可以很容易地替换 g1 的顶部刻面 strip 来自 g2
library(grid)
library(gtable)
gt1 = ggplot_gtable(ggplot_build(g1))
gt2 = ggplot_gtable(ggplot_build(g2))
gt1$grobs[grep('strip-t.+1$', gt1$layout$name)] = gt2$grobs[grep('strip-t', gt2$layout$name)]
grid.draw(gt1)

enter image description here

添加右侧面板条需要我们首先在网格布局中添加一个新列,然后将相关条条粘贴到其中:
gt.side1 = gtable_filter(gt2, 'strip-r-1')
gt.side2 = gtable_filter(gt2, 'strip-r-2')
gt.side3 = gtable_filter(gt2, 'strip-r-3')

gt1 = gtable_add_cols(gt1, widths=gt.side1$widths[1], pos = -1)
gt1 = gtable_add_grob(gt1, zeroGrob(), t = 1, l = ncol(gt1), b=nrow(gt1))

panel_id <- gt1$layout[grep('panel-.+1$', gt1$layout$name),]
gt1 = gtable_add_grob(gt1, gt.side1, t = panel_id$t[1], l = ncol(gt1))
gt1 = gtable_add_grob(gt1, gt.side2, t = panel_id$t[2], l = ncol(gt1))
gt1 = gtable_add_grob(gt1, gt.side3, t = panel_id$t[3], l = ncol(gt1))

grid.newpage()
grid.draw(gt1)

enter image description here

关于r - 如何像在 facet_grid 中一样在 facet_wrap 中定位 strip 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52706599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com