gpt4 book ai didi

r - ggplot2 中 geom_point 的 npc 坐标

转载 作者:行者123 更新时间:2023-12-03 15:10:45 27 4
gpt4 key购买 nike

如何在 ggplot 中获取 geom_point 的 x、y 坐标,其中引用系是整个绘制的图像?

我可以使用以下命令创建一个带有一些 geom_points 的 ggplot:

library(ggplot2)

my.plot <- ggplot(data.frame(x = c(0, 0.456, 1), y = c(0, 0.123, 1))) +
geom_point(aes(x, y), color = "red")

这给出:

enter image description here

通过将其转换为 grob,我可以提取有关此 ggplot 的一些附加信息,例如相对于绘图面板的坐标,由紫色箭头标记。但是,这忽略了轴占用的空间。
my.grob <- ggplotGrob(my.plot)
my.grob$grobs[[6]]$children[[3]]$x
# [1] 0.0454545454545455native 0.46native 0.954545454545454native
my.grob$grobs[[6]]$children[[3]]$y
# [1] 0.0454545454545455native 0.157272727272727native 0.954545454545454native

当我从整个图像的左下角开始测量时,如何获得 x、y 坐标的值,用绿色箭头标记?

如果可能的话,我希望解决方案考虑到 ggplot 的主题。添加像 + theme_void() 这样的主题会影响轴,并且还会相对于整个绘制图像移动点的位置。

更新 :
我意识到轴的字体大小会根据绘图的宽度和高度而变化,从而影响绘图面板的相对大小。因此,在不定义绘图宽度和绘图高度的情况下以 npc 为单位提供位置并非易事。如果可能,将 geom_points 的位置作为绘图宽度和绘图高度的函数。

最佳答案

当您调整 ggplot 的大小时,面板中元素的位置在 npc 空间中不是固定位置。这是因为绘图的某些组件具有固定大小,而其中一些组件(例如面板)会根据设备的大小更改尺寸。

这意味着任何解决方案都必须考虑设备大小,如果您想调整绘图大小,则必须再次运行计算。话虽如此,对于大多数应用程序(包括您的应用程序),这不是问题。

另一个困难是确保您在面板 grob 中识别出正确的 grob,并且很难看出如何轻松地概括这一点。在您的示例中使用列表子集函数 [[6]][[3]] 不能推广到其他绘图。

无论如何,该解决方案的工作原理是测量 gtable 中的面板尺寸和位置,然后将所有尺寸转换为毫米,然后再除以以毫米为单位的绘图尺寸以转换为 npc 空间。我试图通过按名称而不是数字索引提取面板和点来使其更通用。

library(ggplot2)
library(grid)
require(gtable)

get_x_y_values <- function(gg_plot)
{
img_dim <- grDevices::dev.size("cm") * 10
gt <- ggplot2::ggplotGrob(gg_plot)
to_mm <- function(x) grid::convertUnit(x, "mm", valueOnly = TRUE)
n_panel <- which(gt$layout$name == "panel")
panel_pos <- gt$layout[n_panel, ]
panel_kids <- gtable::gtable_filter(gt, "panel")$grobs[[1]]$children
point_grobs <- panel_kids[[grep("point", names(panel_kids))]]
from_top <- sum(to_mm(gt$heights[seq(panel_pos$t - 1)]))
from_left <- sum(to_mm(gt$widths[seq(panel_pos$l - 1)]))
from_right <- sum(to_mm(gt$widths[-seq(panel_pos$l)]))
from_bottom <- sum(to_mm(gt$heights[-seq(panel_pos$t)]))
panel_height <- img_dim[2] - from_top - from_bottom
panel_width <- img_dim[1] - from_left - from_right
xvals <- as.numeric(point_grobs$x)
yvals <- as.numeric(point_grobs$y)
yvals <- yvals * panel_height + from_bottom
xvals <- xvals * panel_width + from_left
data.frame(x = xvals/img_dim[1], y = yvals/img_dim[2])
}

现在我们可以用你的例子来测试它:

my.plot <- ggplot(data.frame(x = c(0, 0.456, 1), y = c(0, 0.123, 1))) +
geom_point(aes(x, y), color = "red")

my.points <- get_x_y_values(my.plot)
my.points
#> x y
#> 1 0.1252647 0.1333251
#> 2 0.5004282 0.2330669
#> 3 0.9479917 0.9442339

我们可以通过在你的红点上绘制一些点来确认这些值是正确的,使用我们的值作为 npc 坐标:

my.plot
grid::grid.draw(pointsGrob(x = my.points$x, y = my.points$y, default.units = "npc"))



reprex package (v0.3.0) 于 2020-03-25 创建

关于r - ggplot2 中 geom_point 的 npc 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60803424/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com