我正在創建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」。 –