gpt4 book ai didi

r - 将网格线与点阵图形中的轴刻度对齐

转载 作者:行者123 更新时间:2023-12-04 15:34:19 24 4
gpt4 key购买 nike

如果选择自定义网格线,是否可以对齐网格线?
默认情况下, panel.grid(h=-1,v=-1) 完成这项工作,但我想根据轴刻度细化网格。将 h 和 v 设置为轴刻度的负长度并不总是有效。

更新:下面是一个例子。我想实现水平网格线与 y 轴刻度相匹配。目前缺少一半的水平网格线。

library(lattice)

G <- cut(trees$Girth, seq(8, 22, by = 3))
p <- xyplot(Height ~ Volume | G, data = trees,
scales = list(x = list(alternating = 3, at = seq(10, 60, by = 5)),
y = list(alternating = 3, at = seq(65, 85, by = 2.5))),
panel = function(x, y, ...){
panel.grid(v = -11, h = -9)
panel.xyplot(x, y, ...)})
print(p)

我目前得到的是:

enter image description here

最佳答案

如果你想在 pretty 返回的地方以外的地方打勾和网格线,然后不要使用 panel.grid ;改为使用 panel.abline ,随您定制 at序列。

library(lattice)

G <- cut(trees$Girth, seq(8, 22, by = 3))
x.at = 5*(2:12)
y.at = 2.5*(26:34)
p <- xyplot(Height ~ Volume | G, data = trees,
scales = list(x = list(alternating = 3, at = x.at),
y = list(alternating = 3, at = y.at)),
panel = function(x, y, ...){
panel.abline(v = x.at, h = y.at, col="lightgrey")
panel.xyplot(x, y, ...)})
print(p)

并且不要忘记将颜色设置为适当的浅色。

enter image description here
panel.grid 的限制在 help(panel.grid) 中被深深提及:

h, v
For panel.abline, these are numeric vectors giving locations respectively of horizontal and vertical lines to be added to the plot, in native coordinates.

For panel.grid, these usually specify the number of horizontal and vertical reference lines to be added to the plot. Alternatively, they can be negative numbers. h=-1 and v=-1 are intended to make the grids aligned with the axis labels. This doesn't always work; all that actually happens is that the locations are chosen using pretty, which is also how the label positions are chosen in the most common cases (but not for factor variables, for instance). h and v can be negative numbers other than -1, in which case -h and -v (as appropriate) is supplied as the n argument to pretty.



(我用斜体表示强调)。

多一条皱纹

如果您使用 scale 对轴应用了变换列表,您必须对 at 应用相同的转换您提供给 panel.abline 的矢量- 我不知道如何让它自动发生。

关于r - 将网格线与点阵图形中的轴刻度对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22433227/

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