gpt4 book ai didi

r - 在 native 坐标系中添加网格单位

转载 作者:行者123 更新时间:2023-12-04 11:33:23 25 4
gpt4 key购买 nike

我正在创建 lattice数字并用 grid 注释它们包裹。要为我的人物设置坐标,我使用 unit()以及来自grid 的相关功能包裹。它通常有助于将单位添加在一起,这通常没有问题。但是我发现当我尝试添加原生单位并且当前视口(viewport)的 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)当它以 native 单位添加时。所以,在这个例子中,我希望 unit(10, "native") + unit(5, "native")将产生一个单位 15native ,但它确实产生了 (15-10)native 的单位。

为什么单位加法在 native 坐标系中以这种方式工作,为什么它在其他坐标系中以不同方式工作?

最佳答案

Josh O'Brien在他的评论中指出了答案,Paul 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

关于r - 在 native 坐标系中添加网格单位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com