将文本框添加到ggplot2中的分面包装布局 [英] Add textbox to facet wrapped layout in ggplot2

查看:138
本文介绍了将文本框添加到ggplot2中的分面包装布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一个人可以注释由ggplot2创建的图,甚至可以组合大视口和小视口,如ggplot2-book中所述.但是,这些似乎只能在实际的绘图区域中起作用,而不能在最终绘图"中起作用.

I am aware that one is able to annotate a plot created by ggplot2 or even to combine large and small viewports, as is documented in the ggplot2-book. However, it seems that these only work in the actual plot-areas and not in the "final plot".

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

For example I have a plot like this:

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

Here we see ten panels showing a linear regression smoother applied to a binomial dataset, but that´s not the point. Now I want a summary (stored in a dataframe) in form of a text in the lower right of the plot, such as this...

我没有找到任何接近的例子. 任何提示,帮助或评论都将不胜感激!

I did not find any example that comes even close. Any hints, helps or comments are much appreciated!

推荐答案

起步较晚,但是我还没有看到任何扩展到多个空面空间的解决方案,所以去了.

Rather late to the game, but I haven't seen any solution that extends to multiple empty facet spaces, so here goes.

第0步.使用内置的钻石数据集,采样具有2个未填充面的ggplot:

Step 0. Sample ggplot with 2 unfilled facets, using the inbuilt diamonds dataset:

library(ggplot2)

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

第1步.使用ggplotGrob

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

Step 2. (Optional) Get the cell coordinates of the unfilled cells to be used for textbox. You can skip this if you prefer to read off the layout above. In this case the top-left cell would be (16, 8) and the bottom-right cell would be (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步.将文本框作为表格覆盖

Step 3. Overlay textbox as a 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)

这篇关于将文本框添加到ggplot2中的分面包装布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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