【发布时间】:2016-05-26 02:11:05
【问题描述】:
我一直试图在 gtable 单元格中指定 rasterGrobs 的绝对位置,但没有成功。我希望能够使图像的范围与 y 轴上的值对齐。该脚本将钻芯图像与 ggplot2 刻面中绘制的多传感器数据对齐。例如,一个特定的射线照片核心图像需要其顶部为 192 毫米,底部为 1482 毫米,但我希望比例尺从 0 到 1523 毫米。请参阅包含的链接以获取我正在做的示例,但为简单起见,我仅在此处发布了 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 中的面板的解决方案之前进行了大量搜索。也许有人可以提出更优雅的解决方案?
【问题讨论】:
-
菜鸟的建议:与
gtable_show_layout(tt)友好 -
是的,谢谢 Edward R. Mazurek。我一直在使用它来获得正确的新列位置。但是我看不到所有文本覆盖的测量信息。是否可以设置一些参数使其更具可读性?