2015-04-28 81 views
3

我正在創建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)本地單位。

爲什麼單位添加以這種方式與原生座標系統一起工作,爲什麼它與其他座標系統以不同的方式工作?

+2

(1)在調用'convertY()'的前兩次調用中,變量'y2'和'y4'在調用時未定義。你是不是指'y1'和'y2'? (2)我懷疑你的問題是由'vignette(「locndimn」)'回答的。 –

+0

謝謝@ JoshO'Brien。我的原始文章中有一個變量命名問題。我的問題至少在很大程度上是通過'vignette(「locndimn」)'來回答的。我很快就會發布更多關於此的信息。 – user697473

+1

如果你沒有完全回答這個問題:獲得你期望的行爲的方法是使用'convertHeight()'和'convertWidth()'而不是'convertY()'和'convertX()' (當**網格**繪圖函數通過一個單元,它是兩個「本地」單元的總和時,顯然隱式地調用其中的最後兩個)。因此,例如,在您的「奇怪結果」部分中,convertHeight(y2,「native」)將評估爲您想要的值「15native」。 –

回答

1

Josh O'Brien點,在他的評論答案,和保羅的Murrell的「locndimn」小插曲(運行vignette("locdimn")提供細節。像unit(5, "native")一個數量有一個含義,如果指的是座標系中的位置,並有不同的含義,如果它Murrell的規則是「位置是像矢量一樣添加的,尺寸是像長度一樣添加的」,這似乎說明了我在本地座標系中添加單位時得到的結果。

具體而言,我的例子,使用convertHeight產生我預期的結果:

library(lattice) 
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 
convertHeight(y2, "native") # 15native 
+0

感謝您寫下這個答案。無論出於何種原因,即使現在我明白了行爲上的差異,我發現Murrell在向量和維度方面的解釋沒有任何澄清!但小插曲中的例子確實清楚發生了什麼,如果一個人耐心地花費幾分鐘時間研究他們爲什麼會產生他們所做的結果。 –