r - adding grid units in the native coordinate system -


i creating lattice figures , annotating them grid package. set coordinates figures, use unit() , related functions grid package. helps add units together, , typically no problem. find strange problem arises when try add native units , x- , y-scales current viewport don't have lower bound of 0. here small example:

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, 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 same lattice plot 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 

further investigation reveals unit() subtracting min(ylim) when addition in native units. so, in example, expect unit(10, "native") + unit(5, "native") yield unit of 15native, yields unit of (15-10)native.

why unit addition work way native coordinate system, , why work different way other coordinate systems?

josh o'brien points answer in comments, , paul murrell's "locndimn" vignette (run vignette("locdimn") provides details. quantity unit(5, "native") has 1 meaning if refers location in coordinate system, , different meaning if refers dimension. murrell's rule "locations added vectors , dimensions added lengths," , seems account results got when adding units in native coordinate system.

specifically, in example, using convertheight produces result expected:

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