将网格线与点阵图形中的轴刻度对齐 [英] Aligning grid lines to axis ticks in lattice graphics

查看:85
本文介绍了将网格线与点阵图形中的轴刻度对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to align grid lines if they are chosen custom? By default, panel.grid(h=-1,v=-1) does the job, but I'd like to refine the grid according to the axis ticks. Setting h and v to negative lengths of axis ticks doesn't always work.

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

Update: Below is an example. I would like to achieve that horizontal grid lines match the y-axis ticks. Currently half of horizontal grid lines are missing.

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)

我现在得到的是这样的:

What I currently get is this:

推荐答案

如果要在除pretty返回的位置之外的其他地方使用刻度线和网格线,请不要使用panel.grid;否则,请使用panel.grid.而是将panel.abline与您的自定义at序列一起使用.

If you want ticks and grid lines at places other than those returned by pretty, then don't use panel.grid; instead use panel.abline, with your custom at sequences.

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)

不要忘记将颜色设置为适当的浅色.

And don't forget to set the colour to something suitably light.

help(panel.grid)的最下方提到了panel.grid的限制:

The limitation with panel.grid is mentioned deep down in help(panel.grid):

h,v
对于panel.abline,这些是数值向量,分别提供水平和垂直位置 以本机坐标添加到绘图中的线.

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.

对于panel.grid,这些通常指定水平和垂直 要添加到绘图中的垂直参考线.或者,他们 可以是负数. h = -1和v = -1旨在形成网格 与轴标签对齐. 这并不总是有效;所有的 实际上发生的是,使用漂亮的位置选择了位置 在最常见的情况下也是如何选择标签位置的 (例如,不适用于因子变量). h和v可以为负 -1以外的数字,在这种情况下-h和-v(视情况而定)是 作为p的n参数提供.

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.

(需要强调的是我的斜体).

(My italics for emphasis).

又一次皱纹

如果已对具有scale列表的轴应用了转换,则必须对提供给panel.ablineat矢量应用相同的转换-我不知道如何将其转换为自动发生.

If you have applied a transformation to an axis with a scale list, you have to apply the same transformation to the at vector that you supply to panel.abline - I don't know how to get this to happen automatically.

这篇关于将网格线与点阵图形中的轴刻度对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆