rasterGrobs 在 gtable 单元格中的绝对定位
Absolute positioning of rasterGrobs in gtable cells
我一直在尝试为 gtable 单元格中的 rasterGrobs 指定绝对位置,但没有成功。我希望能够使图像的范围与 y 轴上的值对齐。该脚本将钻芯图像与绘制在 ggplot2 方面的多传感器数据对齐。例如,一张特定的射线照片核心图像需要其顶部为 192 毫米,底部为 1482 毫米,但我希望比例从 0 到 1523 毫米。请参阅随附的 link 以了解我正在做的事情的示例,但为简单起见,我只在此处发布了一个 MWE。是否可以为 gtable 单元格内的 rasterGrob 指定绝对位置?
就下面的 MWE 而言,到目前为止,我唯一的解决方案是使用 rasterGrob()
时设置的相对位置移动 Rlogo.png
。使用 "native"
坐标似乎也不是我所需要的。同样,我无法理解 gtable_add_grob()
.
中调用的位置参数
library(png)
library(ggplot2)
library(gtable)
# read Image
img <- readPNG(system.file("img", "Rlogo.png", package = "png"))
# convert to rastergrob
g <- rasterGrob(img, y = unit(0.5, "npc"), x = unit(0.5, "npc"))
# create plot
tp <- qplot(1:5, 1:5, geom="blank") + scale_y_reverse()
# convert plot to gtable
tt <- ggplot_gtable(ggplot_build(tp))
# add column to gtable to hold image
tt <- gtable_add_cols(tt, tt$width[[.5*4]], 3)
# add grob to cell 3, 4
tt <- gtable_add_grob(tt,g,3,4)
# render
grid.draw(tt)
在提出使用 rasterGrob 将图像添加到 ggplot 面板的解决方案之前进行了大量搜索。也许有人可以建议更优雅的解决方案?
grob 可以设置它在单元格中的位置,如下图所示
library(gridExtra)
library(grid)
library(gtable)
# quick shortcut to create a 2x2 gtable filled with 4 rectGrobs
tg <- arrangeGrob(grobs=replicate(4, rectGrob(), FALSE))
# red rect of fixed size with default position (0.5, 0.5) npc
rg1 <- rasterGrob("red", width=unit(1,"cm"), height=unit(1,"cm"))
# blue rect with specific x position (0) npc, left-justified
rg2 <- rasterGrob("blue", width=unit(1,"cm"), height=unit(1,"cm"),
x = 0, hjust=0)
# green rect at x = 1cm left-justified, y=-0.5cm from middle, top-justified
rg3 <- rasterGrob("green", width=unit(1,"cm"), height=unit(1,"cm"),
x = unit(1,"cm"), y=unit(0.5, "npc") - unit(0.5, "cm"),
hjust=0, vjust=1)
# place those on top
tg <- gtable_add_grob(tg, rg1, 1, 2, z = Inf, name = "default")
tg <- gtable_add_grob(tg, rg2, 1, 2, z = Inf, name = "left")
tg <- gtable_add_grob(tg, rg3, 1, 2, z = Inf, name = "custom")
grid.newpage()
grid.draw(tg)
我一直在尝试为 gtable 单元格中的 rasterGrobs 指定绝对位置,但没有成功。我希望能够使图像的范围与 y 轴上的值对齐。该脚本将钻芯图像与绘制在 ggplot2 方面的多传感器数据对齐。例如,一张特定的射线照片核心图像需要其顶部为 192 毫米,底部为 1482 毫米,但我希望比例从 0 到 1523 毫米。请参阅随附的 link 以了解我正在做的事情的示例,但为简单起见,我只在此处发布了一个 MWE。是否可以为 gtable 单元格内的 rasterGrob 指定绝对位置?
就下面的 MWE 而言,到目前为止,我唯一的解决方案是使用 rasterGrob()
时设置的相对位置移动 Rlogo.png
。使用 "native"
坐标似乎也不是我所需要的。同样,我无法理解 gtable_add_grob()
.
library(png)
library(ggplot2)
library(gtable)
# read Image
img <- readPNG(system.file("img", "Rlogo.png", package = "png"))
# convert to rastergrob
g <- rasterGrob(img, y = unit(0.5, "npc"), x = unit(0.5, "npc"))
# create plot
tp <- qplot(1:5, 1:5, geom="blank") + scale_y_reverse()
# convert plot to gtable
tt <- ggplot_gtable(ggplot_build(tp))
# add column to gtable to hold image
tt <- gtable_add_cols(tt, tt$width[[.5*4]], 3)
# add grob to cell 3, 4
tt <- gtable_add_grob(tt,g,3,4)
# render
grid.draw(tt)
在提出使用 rasterGrob 将图像添加到 ggplot 面板的解决方案之前进行了大量搜索。也许有人可以建议更优雅的解决方案?
grob 可以设置它在单元格中的位置,如下图所示
library(gridExtra)
library(grid)
library(gtable)
# quick shortcut to create a 2x2 gtable filled with 4 rectGrobs
tg <- arrangeGrob(grobs=replicate(4, rectGrob(), FALSE))
# red rect of fixed size with default position (0.5, 0.5) npc
rg1 <- rasterGrob("red", width=unit(1,"cm"), height=unit(1,"cm"))
# blue rect with specific x position (0) npc, left-justified
rg2 <- rasterGrob("blue", width=unit(1,"cm"), height=unit(1,"cm"),
x = 0, hjust=0)
# green rect at x = 1cm left-justified, y=-0.5cm from middle, top-justified
rg3 <- rasterGrob("green", width=unit(1,"cm"), height=unit(1,"cm"),
x = unit(1,"cm"), y=unit(0.5, "npc") - unit(0.5, "cm"),
hjust=0, vjust=1)
# place those on top
tg <- gtable_add_grob(tg, rg1, 1, 2, z = Inf, name = "default")
tg <- gtable_add_grob(tg, rg2, 1, 2, z = Inf, name = "left")
tg <- gtable_add_grob(tg, rg3, 1, 2, z = Inf, name = "custom")
grid.newpage()
grid.draw(tg)