在R格子xyplot上分别控制轴刻度和轴线 [英] Controlling axis ticks and axis lines separately on R lattice xyplot

查看:360
本文介绍了在R格子xyplot上分别控制轴刻度和轴线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在保持轴刻度刻度线的同时删除xyplot周围的框?按照爱德华·塔夫特(Edward Tufte)的极简主义数据图形美学的精神,这些轴线是非数据墨水",可以(应该吗?)擦除".

How can I go about removing the box around an xyplot, while keeping the axis scale tick marks? In the spirit of Edward Tufte's minimalist data graphic aesthetic, these axis lines are "non-data ink," and can (should?) be "erased."

library(lattice)
my.df <- data.frame(x=-10:10)
my.df$y <- my.df$x^2
xyplot(y~x,data=my.df)

似乎网格显示参数(例如axis.line$col)同时控制了轴线和轴刻度:

It seems that the trellis display parameters (e.g. axis.line$col) control both the axis lines and axis ticks together:

xyplot(y~x,data=my.df,
       par.settings=list(axis.line=list(col="transparent")))

...这不是理想的结果,因此看起来好像没有一种简单的方法可以在离开包装盒时关闭线路.

...which is not the desired result, so it doesn't look like there's a simple way to turn off the lines while leaving the box.

我能想到的最好的方法是蛮力破解,我可以在其中使用panel.segments手动创建刻度线:

The best I've been able to come up with is a brute-force hack, where I build the tick marks by hand using panel.segments:

at.x=pretty(c(-10,10))
at.y=pretty(c(0,100))
xyplot(y~x,data=my.df,
       par.settings=list(axis.line=list(col="transparent")),
       scales=list(x=list(at=at.x,labels=at.x),
       y=list(at=at.y,labels=at.y)),
       panel=function(...){
           panel.xyplot(...)
           panel.segments(x0=at.x,x1=at.x,y0=-4,y1=-2)
           panel.segments(x0=-11.5,x1=-11,y0=at.y,y1=at.y)
       }
       )

这接近期望的结果,但是要使刻度线具有合理的长度并与数据点偏移很"的距离,还需要做很多事情.这些值不会从一个图形转换为下一个图形.另外,请注意,轴标签现在距刻度线的距离太远了.我敢肯定,有一种方法可以减少这种填充,但这只会使代码更加丑陋且不便于移植.

This is close to the desired result, but there's quite a bit of fiddling required to get the tick marks to be a reasonable length and offset a "nice" distance from the data points. These values won't translate from one graphic to the next. Plus, note that the axis labels are now padded too far from the tick marks. I'm sure there's a way to reduce that padding, but that would only make the code even uglier and less portable.

那么,如何在保留刻度线和轴标签完好无损的情况下,只压制构成绘图区框"的线呢?如果这种方法也可以用来压制部分但不是全部线条(例如,保留左侧和下方的线条,而压制顶部和右侧的线条),则可以得到加分.

So how can one go about suppressing just the lines that make up the "box" around the plot area, while leaving the tick marks and axis labels intact? Bonus points if this approach could also be used to suppress some, but not all of the lines (e.g. leave the left and lower lines, but suppress the top and right lines).

推荐答案

这还是有点棘手,但至少您不必手动进行任何计算.它结合使用par.settings和自定义axis函数,该函数接受参数line.col并通过调用trellis.par.set临时更改轴线颜色:

This is still a bit hacky, but at least you don't have to do any figuring by hand. It uses a combination of par.settings and a custom axis function that takes an argument line.col and temporarily changes the axis line color by a call to trellis.par.set:

编辑(删除了不必要的网格设置更改)

EDIT (removed unnecessary changing of trellis settings)

xyplot(y~x,data=my.df, par.settings = list(axis.line = list(col = "transparent")),
  # Pass custom axis function to argument 'axis'
  axis = function(side, line.col = "black", ...) {
    # Only draw axes on the left and bottom
    if(side %in% c("left","bottom")) {
      # Call default axis drawing function
      axis.default(side = side, line.col = "black", ...)
    }
  }
)

此刻,我总结了为什么自定义轴函数的自变量中必须使用line.col = "black"魔术.我的猜测是,它与与省略号(...)匹配的参数有关.也许明天我会变得更聪明,找到真正的原因.

At the moment, I chalk up why line.col = "black" is required in the arguments of the custom axis function to magic. My guess is that it has to do with argument matching with the ellipses (...). Perhaps I'll be wiser tomorrow and find the true reason.

结果是:

这篇关于在R格子xyplot上分别控制轴刻度和轴线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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