在ggplot2中将文本框添加到构面包装布局中

Jen*_*ens 10 plot r annotate ggplot2

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

例如,我有一个这样的情节: 在此输入图像描述

在这里,我们看到十个面板显示应用于二项式数据集的线性回归平滑器,但这不是重点.现在我想在图表的右下角以文本的形式提供摘要(存储在数据框中),例如... 在此输入图像描述

我没有找到任何甚至接近的例子.任何提示,帮助或评论都非常感谢!

Z.L*_*Lin 6

比赛的时间已晚,但我还没有看到任何解决方案延伸到多个空方面空间,所以这里就是这样.

第0步.使用内置钻石数据集示例ggplot,其中包含2个未填充的构面:

library(ggplot2)

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

第0步

第1步.使用将绘图转换为gtableggplotGrob

gp <- ggplotGrob(p)

library(gtable)

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

步骤1

第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
Run Code Online (Sandbox Code Playgroud)

第3步.将textbox叠加为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)
Run Code Online (Sandbox Code Playgroud)

第3步

演示一些变化:

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)
Run Code Online (Sandbox Code Playgroud)

演示1

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)
Run Code Online (Sandbox Code Playgroud)

演示2

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)
Run Code Online (Sandbox Code Playgroud)

演示3