gpt4 book ai didi

r - 在 ggplot2 中使用 grconvertX/grconvertY

转载 作者:行者123 更新时间:2023-12-04 06:43:34 27 4
gpt4 key购买 nike

我想弄清楚如何在 ggplot 中使用 grconvertX/grconvertX。我的最终目标是为 添加注释。 ggplot2 图(也可能是 格子 )和 grid.textgrid.lines通过从用户坐标到设备坐标。我知道它可以用 grobs 完成,但我想知道是否有更简单的方法。

以下代码允许我将值从用户坐标传递到 ndc 坐标,并使用这些值用 grid.text 注释绘图.

graphics.off()      # close graphics windows   

library(grid)
library(gridBase)


test= data.frame(
x = c(1,2,3),
y = c(12,10,3),
n = c(75,76,73)
)

par(mar = c(13,5,2,3))

plot(test$y ~ test$x,type="b", ann=F)

for (i in 1:nrow(test))

{
X=grconvertX(i , from="user", to="ndc")
grid.text(x=X, y =0.2, label=paste("GRID.text at\nuser.x=", i, "\n", "ndc.x=", (signif( X, 5)) ) )
grid.lines(x=c(X, X), y = c(0.28, 0.33) )
}
#add some code to save as PDF ...

enter image description here

该代码基于我之前的一篇文章中的解决方案: Mixing X and Y coordinate systems .您可以看到原始绘图中的 x 坐标是如何转换为 ndc 的。这种方法的优点是我可以为 Y 使用设备坐标。

我以为我可以轻松地在 中做同样的事情ggplot2 (可能在 格子 中)。
library(ggplot2)
graphics.off() # close graphics windows

qplot(x=x, y=y, data=test)+geom_line()+ opts(plot.margin = unit(c(1,3,8,1), "lines"))

for (i in 1:nrow(test))

{
X=grconvertX(i , from="user", to="ndc")
grid.text(x=X, y =0.2, label=paste("GRID.text at\nuser.x=", i, "\n", "ndc.x=", (signif( X, 5)) ) )
grid.lines(x=c(X, X), y = c(0.28, 0.33) )
}

#add some code to save as PDF...

但是,它不能正常工作。坐标好像有点不对。垂直线和文本与图上的刻度标签不对应。谁能告诉我如何解决它?提前非常感谢。

enter image description here

最佳答案

grconvertXgrconvertY函数使用基本图形,而 ggplot2 使用网格图形。一般来说,两种不同的图形引擎不能很好地协同工作(尽管您已经演示了使用 gridBase 来提供帮助)。您的第一个示例有效,因为您从基本图形开始,因此用户坐标系与基本图形和grconvertX 一起存在。从中转换。在第二种情况下,用户坐标系从未在基本图形中设置,因此看起来它可能使用默认坐标 0,1,这与顶部视口(viewport)坐标相似但不相同,因此您会得到类似但不完全正确的东西(我真的很惊讶你没有收到错误或警告

通常对于网格图形,在坐标之间进行转换的等价物是仅使用感兴趣的坐标系创建一个新视口(viewport)(或使用正确的坐标系推送/弹出到现有视口(viewport)),然后在该视口(viewport)中添加注释。

这是一个创建绘图的示例,然后向下移动到包含主绘图的视口(viewport),创建一个具有相同尺寸但关闭裁剪的新视口(viewport),x 比例基于数据,y 比例为 0,1 ,然后相应地添加一些文本:

library(ggplot2)
library(grid)

test= data.frame( x = c(1,2,3), y = c(12,10,3), n = c(75,76,73) )

qplot(x=x, y=y, data=test)+geom_line()+ opts(plot.margin = unit(c(1,3,8,1), "lines"))

current.vpTree()
downViewport('panel-3-4')
pushViewport(dataViewport( test$x, clip='off',yscale=c(0,1)))

for (i in 1:nrow(test)) {
grid.text(x=i, y = -0.2, default.units='native',
label=paste("GRID.text at\nuser.x=", i, "\n" ) )
grid.lines(x=c(i, i), y = c(-0.1, 0), default.units='native' )
}

这里棘手的事情之一是 ggplot2 没有设置视口(viewport)比例以匹配正在绘制的数据,而是自己进行转换。在这种情况下,根据 x 数据设置比例是可行的,但如果 ggplot2 做了一些更有趣的事情,那么这可能不起作用。我们需要的是某种方法来从 ggplot2 获取转换后的坐标,以便在对 grid.text 的调用中使用。

关于r - 在 ggplot2 中使用 grconvertX/grconvertY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10536396/

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