我正在创建lattice
数字并使用grid
包对其进行注释。为了设置我的数字坐标,我使用grid
包中的unit()
和相关功能。它通常有助于将单位添加到一起,这通常没有问题。但我发现,当我尝试添加本地单位和x和y尺度当前视口没有一个下界的0.这一个奇怪的问题出现了一个小例子:在原生坐标系中添加网格单元
library(grid)
library(lattice)
# Expected result
xyplot(0:10 ~ 0:10, ylim=c(0,10))
myVP <- seekViewport("plot_01.panel.1.1.vp")
y1 <- unit(5, "native") + unit(2.5, "native")
convertY(y1, "native") # 7.5native, as expected
# Strange result
xyplot(10:20 ~ 0:10, ylim = c(10:20))
myVP <- seekViewport("plot_01.panel.1.1.vp")
y2 <- unit(10, "native") + unit(5, "native")
convertY(y2, "native") # 5native (why not 15native?)
# Other results with same lattice plot are as expected
convertY(unit(10, "npc") + unit(5, "npc"), "npc") # 15npc
convertY(unit(10, "mm") + unit(5, "mm"), "mm") # 15mm
convertY(unit(10, "native") + unit(5, "mm"), "native") # ~10.35native
进一步的调查显示,unit()
减去min(ylim)
时,它以本地单位添加。因此,在这个例子中,我预计unit(10, "native") + unit(5, "native")
将产生15native
的单位,但它确实产生(15-10)本地单位。
为什么单位添加以这种方式与原生坐标系统一起工作,为什么它与其他坐标系统以不同的方式工作?
(1)在调用'convertY()'的前两次调用中,变量'y2'和'y4'在调用时未定义。你是不是指'y1'和'y2'? (2)我怀疑你的问题是由'vignette(“locndimn”)'回答的。 –
谢谢@ JoshO'Brien。我的原始文章中有一个变量命名问题。我的问题至少在很大程度上是通过'vignette(“locndimn”)'来回答的。我很快就会发布更多关于此的信息。 – user697473
如果你没有完全回答这个问题:获得你期望的行为的方法是使用'convertHeight()'和'convertWidth()'而不是'convertY()'和'convertX()' (当**网格**绘图函数通过一个单元,它是两个“本地”单元的总和时,显然隐式地调用其中的最后两个)。因此,例如,在您的“奇怪结果”部分中,convertHeight(y2,“native”)将评估为您想要的值“15native”。 –