【问题标题】:Add textbox to facet wrapped layout in ggplot2将文本框添加到 ggplot2 中的分面包装布局
【发布时间】:2015-02-13 09:53:24
【问题描述】:

我知道可以注释 ggplot2 创建的绘图,甚至可以组合大小视口,如 ggplot2-book 中所述。然而,这些似乎只适用于实际的情节区域,而不是“最终情节”。

例如,我有一个这样的情节:

在这里,我们看到十个面板显示了应用于二项式数据集的线性回归平滑器,但这不是重点。现在我想要一个摘要(存储在数据框中),在图的右下角以文本的形式出现,比如这个......

我没有找到任何更接近的例子。 非常感谢任何提示、帮助或 cmets!

【问题讨论】:

  • 你看过gridExtra包,并使用过ggplotGrob()吗?
  • !!!你是对的,我刚刚根据你的建议检查了gridExtra,看到这篇文章的最后一条评论:r-bloggers.com/extra-extra-get-your-gridextra
  • 原来 gridExtra 相当笨拙。我能够使用 ncol=2 将表格和图形绘制成两行或两列布局,但它并不真正适合需求。我将继续尝试并检查 ggplotGrob 函数。
  • 相关:stackoverflow.com/questions/22450765/…,只需将ggplotGrob替换为tableGrob即可。

标签: r plot ggplot2 annotate


【解决方案1】:

游戏已经很晚了,但我还没有看到任何扩展到多个空白刻面空间的解决方案,所以就这样吧。

第 0 步。示例 ggplot 具有 2 个未填充的面,使用内置的 diamonds 数据集:

library(ggplot2)

p <- ggplot(diamonds,
       aes(x = carat, y = price)) +
  geom_point() +
  geom_smooth() +
  facet_wrap(~color)
p

第 1 步。使用 ggplotGrob 将绘图转换为 gtable

gp <- ggplotGrob(p)

library(gtable)

# visual check of gp's layout (in this case, it has 21 rows, 15 columns)
gtable_show_layout(gp)

第 2 步。 (可选)获取用于文本框的未填充单元格的单元格坐标。如果你喜欢阅读上面的布局,你可以跳过这个。在这种情况下,左上角的单元格为 (16, 8),右下角的单元格为 (18, 12)。

# get coordinates of empty panels to be blanked out
empty.area <- gtable_filter(gp, "panel", trim = F)
empty.area <- empty.area$layout[sapply(empty.area$grob,
                                       function(x){class(x)[[1]]=="zeroGrob"}),]

empty.area$t <- empty.area$t - 1 #extend up by 1 cell to cover facet header
empty.area$b <- empty.area$b + 1 #extend down by 1 cell to cover x-axis

> empty.area
   t  l  b  r z clip      name
6 16  8 18  8 1   on panel-3-2
9 16 12 18 12 1   on panel-3-3

第 3 步。将文本框叠加为 tableGrob

library(gridExtra)

gp0 <- gtable_add_grob(x = gp,
                       grobs = tableGrob("some text",
                                         theme = ttheme_minimal()),
                       t = min(empty.area$t), #16 in this case
                       l = min(empty.area$l), #8
                       b = max(empty.area$b), #18
                       r = max(empty.area$r), #12
                       name = "textbox")
grid::grid.draw(gp0)

展示一些变化:

gp1 <- gtable_add_grob(x = gp,
                       grobs = tableGrob("Simple line of comment that can go on & on for the sake of demonstration. Automatic line wrap not included.",
                                         theme = ttheme_minimal()),
                       t = min(empty.area$t),
                       l = min(empty.area$l),
                       b = max(empty.area$b),
                       r = max(empty.area$r),
                       name = "textbox")
grid::grid.draw(gp1)

gp2 <- gtable_add_grob(x = gp,
                       grobs = tableGrob("Simple line of comment that can go on & on. 
Automatic line wrap not included. \nAt least it understands the concept of line breaks.",
                                         theme = ttheme_minimal()),
                       t = min(empty.area$t),
                       l = min(empty.area$l),
                       b = max(empty.area$b),
                       r = max(empty.area$r),
                       name = "textbox")
grid::grid.draw(gp2)

gp3 <- gtable_add_grob(x = gp,
                       grobs = tableGrob(tibble::tribble(~col1, ~col2,
                                                         "a.", "This is a line in a table",
                                                         "b.", "This is another line in a table"),
                                         rows = NULL,
                                         theme = ttheme_minimal()),
                       t = min(empty.area$t),
                       l = min(empty.area$l),
                       b = max(empty.area$b),
                       r = max(empty.area$r),
                       name = "textbox")
grid::grid.draw(gp3)

【讨论】:

  • 太棒了!迟到但非常受欢迎!这是我仍然遇到的一个问题,但没有找到解决方案。非常感谢林!
  • 太棒了!谢谢!如果想在空白处添加一个新图形(而不是文本或表格),在最后一步,只需将“gtable_and_grob()”函数中的“grobs”参数更改为 ggplotGrob(my_other_plot)。
【解决方案2】:

从 ggplot2 2.2 开始,我改用了标题选项。您可能想尝试一下。

library(tidyverse)

ggplot(data = mtcars, 
            mapping = aes(y=mpg, x=wt)) +
  geom_point() +
  facet_wrap(c("gear", "cyl"), labeller = "label_both") +
  labs(title = "mtcars analysis",
       subtitle = "This is a subtitle \n",
       caption = "\n Note: This analysis compares the number of gears and cylinders", 
       x = "weight", y = "mpg")

这就是你会得到的:

希望对你有帮助。

【讨论】:

  • 有趣,但标题在图的底部,并且没有填满图的右下角。不过,感谢您的贡献。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
  • 2012-02-04
  • 1970-01-01
相关资源
最近更新 更多