如何像在 facet_grid 中一样在 facet_wrap 中定位条带标签 [英] How to position strip labels in facet_wrap like in facet_grid

查看:17
本文介绍了如何像在 facet_grid 中一样在 facet_wrap 中定位条带标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用 facet_wrap() 和使用两个变量和两个自由缩放的刻面时去除条带标签的冗余.

例如下图的这个facet_wrap版本

库(ggplot2)dt <- txhousing[txhousing$year %in% 2000:2002 &txhousing$month %in% 1:3,]ggplot(dt, aes(median, sales)) +geom_point() +facet_wrap(c("年", "月"),labeller = "label_both",秤=免费")

应该具有 facet_grid 版本的外观,其中条形标签位于图形的顶部和右侧边缘(也可以是底部和左侧边缘).

ggplot(dt, aes(median, sales)) +geom_point() +facet_grid(c("年", "月"),labeller = "label_both",秤=免费")

不幸的是,使用 facet_grid 不是一种选择,因为据我所知,它不允许秤完全免费" - 请参阅

我对上述 hackish 的尝试没有意见,但我想知道 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") +主题(strip.background = element_blank(),strip.text = element_blank())g2 = ggplot(dt, aes(median, sales)) +geom_point() +facet_grid(c("year", "month"), scales = "free")

现在我们可以很容易地将 g1 的顶部刻面条替换为 g2

库(网格)图书馆(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)

添加右侧面板条需要我们先在网格布局中添加一个新列,然后将相关条条粘贴到其中:

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, 宽度=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)

I would like to remove the redundancy of strip labels when using facet_wrap() and faceting with two variables and both scales free.

For example, this facet_wrap version of the following graph

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")

should have the looks of this facet_grid version of it, where the strip labels are at the top and right edge of the graph (could be bottom and left edge as well).

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

Unfortunately, using facet_grid is not an option because, as far as I understand, it doesn't allow scales to be "completely free" - see here or here

One attempt that I thought about would be to produce separate plots and then combine them:

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)

I am ok with the above hackish attempt, but I wonder if there is something in facet_wrap that could allow the desired output. I feel that I miss something obvious about it and maybe my search for an answer didn't include the proper key words (I have the feeling that this question was addressed before).

解决方案

This does not seem easy, but one way is to use grid graphics to insert panel strips from a facet_grid plot into one created as a facet_wrap. Something like this:

First lets create two plots using facet_grid and 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")

Now we can fairly easily replace the top facet strips of g1 with those from 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)

Adding the right hand panel strips need us to first add a new column in the grid layout, then paste the relevant strip grobs into it:

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)

这篇关于如何像在 facet_grid 中一样在 facet_wrap 中定位条带标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆