6

The core of my problem:

I'm attempting to convert npc units to native units using the grid package's convertUnit, convertX and convertY functions. (npc=normalized parent coordinates, possibly known as ndc units, normalized device coordinates to some in base graphics R. I'm trying to get to native units, those in which the plot is graphed, so in terms of the xlim and ylim units.) However when I attempt to do this as such:

> xyplot(1:10~1:10)
> convertX(unit(.9, "npc"), "native")
[1] 484.2native

when I'm expecting a number close to 9 as the native x coordinate. It appears convertX is returning units in device coordinates/pixels instead.

Reasoning: I'm trying to use a base locator type device to return npc coordinates, and from those npc coordinates convert to the native coordinates in which the graph was plotted. While I can use base graphics' locator or grid.locator, I'm trying to extend the functionality of this new, non blocking locator to grid/lattice graphics by converting from npc back to native. convertUnit and convertY don't work either.

Question Is it possible for grid to convert from npc back to the active plotting window's native coordinates? Why is convertX returning pixels rather than native coordinates?

Thanks much in advance.

Edited for tags and sloppy mistake leaving out xyplot before. My apologies, but it holds with xyplot.

4

2 回答 2

1

'"native"' 位置和尺寸与视口的 'xscale' 和 'yscale' 相关。转换发生在当前视口内。

> plot(1:10)
> convertX(unit(.9,"npc"),"native")
[1] 453.6native
> pushViewport(viewport())
> convertX(unit(.9,"npc"),"native")
[1] 0.9native
> convertX(unit(.1,"npc"),"picas")
[1] 4.21575picas #making window smaller
> convertX(unit(.1,"npc"),"picas")
[1] 1.9798375984252picas #making window larger
> convertX(unit(.1,"npc"),"picas")
[1] 5.25783218503937picas

因此,您首先需要一个视口来获取合理的值。

于 2011-08-12T21:12:01.593 回答
0

显然,视口被推送后,它忘记了底层绘图的设置坐标,新坐标似乎等同于 npc 坐标。

直到您绘制到新视口中,之后您又回到了第一方:

xyplot(1:10 ~ 10:1)
> convertX(unit(.9, "npc"), "native")
[1] 605.7native
> pushViewport(viewport())
> convertX(unit(.9, "npc"), "native")
[1] 0.9native
> xyplot(1:10 ~ 10:1)
> convertX(unit(.9, "npc"), "native")
[1] 605.7native

是否可以获得与实际绘制 x 和 y 的坐标相对应的坐标?

于 2015-07-09T18:36:40.563 回答