r - Lattice 的 `panel.rug` 产生不同的线长度和宽图

标签 r data-visualization lattice

当在边距包含 panel.rug() 的点阵中生成宽图时,崎岖边距中的线条长度在 y 轴上比x 轴:

library(lattice)
png(width=800, height=400)
xyplot(Fertility ~ Education, swiss, panel = function(x, y,...) {
  panel.xyplot(x, y, col=1, pch=16)
  panel.rug(x, y, col=1, end= ...)})
dev.off()

enter image description here

我希望 x 轴和 y 轴上的地毯线长度相同,无论绘图的形状如何(注意:现在地毯线仅当绘图为正方形时长度才会相同)。

最佳答案

使用lattice,只需将panel.rug使用的坐标系从默认值(“npc”)更改为“snpc":

library(lattice)

## png(width=800, height=400)
xyplot(Fertility ~ Education, swiss, panel = function(x, y,...) {
  panel.xyplot(x, y, col=1, pch=16)
  panel.rug(x = x, y = y,  
            x.units = rep("snpc", 2),  y.units = rep("snpc", 2), 
            col=1, end= ...)
})
## dev.off()

enter image description here

要了解为什么这会得到您想要的结果,请参阅?unit以获取这两个坐标系含义的描述:

 Possible ‘units’ (coordinate systems) are:

 ‘"npc"’ Normalised Parent Coordinates (the default).  The origin
      of the viewport is (0, 0) and the viewport has a width and
      height of 1 unit.  For example, (0.5, 0.5) is the centre of
      the viewport.

 ‘"snpc"’ Square Normalised Parent Coordinates.  Same as Normalised
      Parent Coordinates, except gives the same answer for
      horizontal and vertical locations/dimensions.  It uses the
      _lesser_ of npc-width and npc-height.  This is useful for
      making things which are a proportion of the viewport, but
      have to be square (or have a fixed aspect ratio).

关于r - Lattice 的 `panel.rug` 产生不同的线长度和宽图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33222841/

相关文章:

重复相同的栅格层以创建栅格堆栈

r - 匹配并提取r中的子字符串

javascript - 用于可视化的假分布数据

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

r - checkError(res) : Undefined error in httr call. httr 输出中的错误:无法连接到本地主机端口 4445:连接被拒绝

regex - 用不同的替换顺序替换字符串中匹配单个模式的多个位置

python - 在 Ruby/Python 中可视化 x、y、z 坐标?

javascript - 如何在 d3 力定向布局节点上显示工具提示?

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

r - 将误差线添加到具有多个组的条形图中