Lattice's `panel.rug` produces different line length with wide plot

家住魔仙堡 提交于 2019-12-11 10:31:13

问题


When producing a wide plot in lattice with margins that include panel.rug(), the length of the lines in rugged margins is longer in the y-axis than x-axis:

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()

I would like those rug lines in x- and y-axes to be the same length regardless of the shape of a plot (note: right now the rug lines will only be the same length when the plot is square).


回答1:


With lattice, just change the coordinate system used by panel.rug from its default ("npc") to "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()

To see why this gets you what you want, refer to ?unit for its description of what those two coordinate systems mean:

 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).


来源:https://stackoverflow.com/questions/33222841/lattices-panel-rug-produces-different-line-length-with-wide-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!