在ggplot2中使用grconvertX

在ggplot2中使用grconvertX

本文介绍了在ggplot2中使用grconvertX / grconvertY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何在ggplot中使用grconvertX / grconvertX。我的最终目标是使用 / code>函数使用基本图形,而ggplot2使用网格图形。一般情况下,2种不同的图形引擎并不能很好地协作(尽管你已经证明使用 gridBase 来提供帮助)。你的第一个例子的工作原理是因为你从一个基础图形开始,所以用户坐标系统与基础图存在,并且 grconvertX 从它转换而来。在第二种情况下,用户坐标系从未设置在基本图形中,因此它看起来可能使用默认坐标0,1,它们与顶部视口坐标相似但不相同,因此您可以得到类似但不完全正确的东西(我真的很惊讶,你没有得到一个错误或警告



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



以下示例创建您的绘图,然后向下移动到包含主绘图的视口,创建一个尺寸相同,但剪裁关闭的新视口,x刻度基于数据,y刻度为0,1,然后相应地添加一些文本:库(网格)

测试= 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 \\\
user.x =,i,\\\
))
grid.lines(x = c(i,i),y = c(-0.1,0),default.units ='native')
}

其中一个棘手的问题是,ggplot2不会将视口比例设置为与数据匹配被绘制,但转换本身。在这种情况下,根据x数据设置刻度,但如果ggplot2做了更有趣的事情,那么这可能不起作用。我们需要的是通过某种方式从ggplot2获取后面转换后的坐标,以便在调用grid.text中使用。


I am trying to figure out how to use grconvertX/grconvertX in ggplot. My ultimate goal is to to add annotation to a ggplot2 figure (and possibly lattice) with grid.text and grid.lines by going from user coordinates to device coordinates. I know it can be done with grobs but I am wondering if there is an easier way.

The following code allows me to pass values from user coordinates to ndc coordinates and use those values to annotate the plot with 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 ...

The code is based on the solution from one of my previous posts: Mixing X and Y coordinate systems . You can see how x coordinates from the original plot were converted to ndc. The advantage of this approach is that I can use device coordinates for Y.

I assumed I could easily do the same in ggplot2 (and possibly in lattice).

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...

However, it does not work correctly. The coordinates seem to be a bit off. The vertical lines and text don't correspond to the tick labels on the plot. Can anybody tell me how to fix it? Thanks a lot in advance.

解决方案

The grconvertX and grconvertY functions work with base graphics while ggplot2 uses grid graphics. In general the 2 different graphics engines don't play nicely together (though you have demonstrated using gridBase to help). Your first example works because you started with a base graphic so the user coordinate system exists with the base graph and grconvertX converts from it. In the second case the user coordinate system was never set in the base graphics, so it looks like it might use the default coordinates of 0,1 which are similar but not identical to the top viewport coordinates so you get something similar but not exactly correct (I am actually surprised that you did not get an error or warning

Generally for grid graphics the equivalent for converting between coordinates is to just create a new viewport with the coordinate system of interest (or push/pop to an existing viewport with the correct coordinate system), then add your annotations in that viewport.

Here is an example that creates your plot, then moves down to the viewport containing the main plot, creates a new viewport with the same dimensions but with clipping turned off, the x scale is based on the data and the y scale is 0,1, then adds some text accordingly:

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' )
 }

One of the tricky things here is that ggplot2 does not set the viewport scales to match the data being plotted, but does the conversions itself. In this case setting the scale based on the x data worked, but if ggplot2 does something fancier then this might not work. What we would need is some way to get the back tranformed coordinates from ggplot2 to use in the call to grid.text.

这篇关于在ggplot2中使用grconvertX / grconvertY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:35