在R格xyplot上分别控制轴刻度和轴线(Controlling axis ticks and axis lines separately on R lattice xyplot)

编程入门 行业动态 更新时间:2024-10-23 17:33:46
在R格xyplot上分别控制轴刻度和轴线(Controlling axis ticks and axis lines separately on R lattice xyplot)

我怎样才能去除围绕xyplot的框,同时保持轴刻度刻度? 本着Edward Tufte极简主义数据图形美学的精神,这些轴线是“非数据墨水”,可以(应该)被“擦除”。

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 )同时控制轴线和轴线滴答:

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

......这不是理想的结果,所以看起来好像没有简单的方法在离开盒子时关闭线条。

我已经能够提出的最好的方法是一种暴力破解,我使用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) } )

这与期望的结果接近,但需要相当多的操作才能使刻度标记的长度合理并偏离距离数据点“很好”的距离。 这些值不会从一个图形转换到另一个图形。 另外请注意,轴标签现在被填充得离刻度线太远。 我确信有一种方法可以减少这种填充,但这只会使代码变得更加丑陋,不便于携带。

那么如何才能在绘图区周围抑制构成“盒子”的线条,同时保留刻度标记和轴标签? 奖励点数,如果这种方法也可以用来抑制一些,但不是所有的线(例如离开左边和下边的线,但抑制顶部和右边的线)。

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)

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.

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临时更改轴线颜色:

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

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" 。 我的猜测是它与椭圆( ... )的参数匹配有关。 也许我明天会更聪明,找到真正的原因。

这导致:

在这里输入图像描述

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", ...) } } )

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.

This results in:

enter image description here

更多推荐

本文发布于:2023-07-25 10:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1260023.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:刻度   轴线   Controlling   xyplot   axis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!