R将布局对象的网格单元转换为 native

标签 r ggplot2 r-grid gtable

我的问题与 Convert units from npc to native using grid in R 有点相关.

我试图弄清楚某些绘图元素的位置从 ggplot2 对象(轴、主图等)开始。我找到了以下代码:

rm(list = ls())
library(ggplot2)
library(grid)
library(gtable)

# a dummy plot
g <- ggplot(cars, aes(x = speed, y = dist)) + 
  geom_point()
g

# a layout of each element
obj <- ggplotGrob(g)
l <- gtable:::gtable_layout(obj)
grid:::grid.show.layout(l)

我需要的所有信息都必须在名为 l 的布局对象中。然而,这个对象的高度和宽度相当奇怪。它们通常为零,即使布局有一些吸引人的地方!我调整了 grid:::grid.show.layout 来打印它正在绘制的尺寸:

# aside from sprintf and cat a copy of grid:::grid.show.layout
foo <- function(l, newpage = TRUE, vp.ex = 0.8, bg = "light grey", 
                cell.border = "blue", cell.fill = "light blue", cell.label = TRUE, 
                label.col = "blue", unit.col = "red", vp = NULL, ...) {
  if (!grid:::is.layout(l)) 
    stop("'l' must be a layout")
  if (newpage) 
    grid.newpage()
  if (!is.null(vp)) 
    pushViewport(vp)
  grid.rect(gp = gpar(col = NULL, fill = bg))
  vp.mid <- viewport(0.5, 0.5, vp.ex, vp.ex, layout = l)
  pushViewport(vp.mid)
  grid.rect(gp = gpar(fill = "white"))
  gp.red <- gpar(col = unit.col)
  objs <- matrix(list(), l$nrow, l$ncol)

  unitType <- "cm"
  for (i in 1L:l$nrow) for (j in 1L:l$ncol) {

    h <- convertX(x = l$heights[i, top = FALSE], unitTo = unitType)
    w <- convertY(x = l$widths[j, top = FALSE], unitTo = unitType)
    s1 <- sprintf("s1: i = %d, j = %d, height = %s, width = %s\n", i, j, h, w)
    cat(s1)

    vp.inner <- viewport(layout.pos.row = i, layout.pos.col = j)
    pushViewport(vp.inner)

    # an attempt so save the drawn objects
    objs[[i, j]] <- grid.rect(gp = gpar(col = cell.border, fill = cell.fill))
    if (cell.label) 
      grid.text(paste0("(", i, ", ", j, ")"), gp = gpar(col = label.col))
    if (j == 1) 
      grid.text(format(l$heights[i, top = FALSE], ...), 
                gp = gp.red, just = c("right", "centre"), x = unit(-0.05, 
                                                                   "inches"), y = unit(0.5, "npc"), rot = 0)
    if (i == l$nrow) 
      grid.text(format(l$widths[j, top = FALSE], ...), 
                gp = gp.red, just = c("centre", "top"), x = unit(0.5, 
                                                                 "npc"), y = unit(-0.05, "inches"), rot = 0)
    if (j == l$ncol) 
      grid.text(format(l$heights[i, top = FALSE], ...), 
                gp = gp.red, just = c("left", "centre"), x = unit(1, 
                                                                  "npc") + unit(0.05, "inches"), y = unit(0.5, 
                                                                                                          "npc"), rot = 0)
    if (i == 1) 
      grid.text(format(l$widths[j, top = FALSE], ...), 
                gp = gp.red, just = c("centre", "bottom"), x = unit(0.5, 
                                                                    "npc"), y = unit(1, "npc") + unit(0.05, "inches"), 
                rot = 0)
    popViewport()
  }
  popViewport()
  if (!is.null(vp)) 
    popViewport()
  return(objs)
}

运行 foo(l) 打印:

s1: i = 1, j = 1, height = 0.193302891933029cm, width = 0.193302891933029cm
...
s1: i = 7, j = 5, height = 0cm, width = 0cm
...
s1: i = 12, j = 9, height = 0.193302891933029cm, width = 0.193302891933029cm

奇怪的是,用browser 单步执行这个函数显示 i = 7, j = 5 打印了中心最大的矩形,然而尺寸是 0cm, 0cm!原始单位(分别为 1null、1null)。

所以我的问题是,我如何获得 npc/native 单位的矩形的大小/坐标?我非常乐意遍历整个结构,但我想将每个矩形的单位变成合理的东西。理想情况下,我为每个布局元素获取由 grid.rect 在 npc 或设备的 native 单位中绘制的四个角的位置。

有什么想法吗?

最佳答案

很抱歉没有完全回答您的问题,但我有一些评论可以提供信息。 null 单位与 0cm0inch 单位不同。 null 单位是一种占位符值:首先放置具有其他单位的所有内容,然后将剩余空间分配给 null 单位对象。这种划分一次发生在一个级别,因此父对象中的 null 单元的解释与子对象中的单元不同。

实际的 null 单位对应的是什么直到绘图被绘制才知道:您可以注意到,如果您在图形设备中调整绘图的大小,轴标题和其他元素通常保持相同的大小,而面板的大小会根据窗口的大小进行调整。

对于所有其他目的,例如转换为其他单位,它们具有零宽度/零高度,因为首先计算其他所有内容,这解释了为什么在函数中转换这些单位时会发现零单位。

因此,除非您有精确的、预定义的绘图尺寸,否则您无法知道“空”单位是什么。

编辑:您的评论是有道理的,我试图找出一种方法来报告以 null 单位定义的面板 grob 的确切宽度和高度,但它依赖于首先绘制绘图,所以它不是先验值。

# Assume g is your plot
gt <- ggplotGrob(g)
is_panel <- grep("panel", gt$layout$name)
# Re-class the panel to a custom class
class(gt$grobs[[is_panel]]) <- c("size_reporter", class(gt$grobs[[is_panel]]))

# The grid package calls makeContent just before drawing, so we can put code 
# here that reports the size
makeContent.size_reporter <- function(x) {
  print(paste0("width: ", convertWidth(x$wrapvp$width, "cm")))
  print(paste0("height: ", convertHeight(x$wrapvp$height, "cm")))
  x
}

grid.newpage(); grid.draw(gt)

现在,每次绘制绘图时,您都会在控制台中收到一条文本,说明实际尺寸的绝对单位(相对于面板的原点)。

关于R将布局对象的网格单元转换为 native ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58391881/

相关文章:

r - 不重叠的抖动点

r - 如何使用ggplot2之类的东西在R中的某些X坐标处创建水平矩形?

r - 尝试映射 geom_vline 的值,但未使用 R 中的 ggplot 在 x 轴上的正确位置绘制

r - 更改堆叠 ggplot 图例中的几何顺序

r - Shiny 的应用程序 : How to get total sum by column

r - 折叠数据框的特定行/案例

r - 美国和加拿大的彩色网格单元格

r - 网格图的常见图例

r - R 程序输出中的常数 e 是什么意思?

r - 如何使用 `facet_nested` 增加为构面标签创建的不同框之间的空间?