绘图区域颜色是否有“par()"设置? [英] Is there a `par()` setting for plot area color?
问题描述
我想知道是否有一种简单的方法可以只为绘图区域着色?我相信 par()$bg
设置定义了整个设备背景的颜色,所以我一直在应用低级绘图命令 polygon
来添加一个整个绘图区域的彩色矩形.
I am wondering if there is an easy way to only color the plot area? I believe the par()$bg
setting defines the color for the background of the entire device, and so I have been applying the low-level plotting command polygon
to add a colored rectangle to the entire plot area.
#colored device background
x11()
par(bg="grey90")
plot(x=1, y=2)
grid(col="white", lty=1)
points(x=1, y=2)
#colored plot area only
x11()
plot(x=1, y=2)
usr <- par()$usr
polygon(x=c(usr[1], usr[1], usr[2], usr[2]), y=c(usr[3], usr[4], usr[4], usr[3]), col="grey90")
grid(col="white", lty=1)
points(x=1, y=2)
这可能是唯一的方法,但如果 par
设置中有更直接的方法,我很想知道.提前感谢您的建议.
This might be the only way to do this, but if there is a more straightforward way in the par
settings, I would love to know. Thanks in advance for any advice.
推荐答案
我很确定没有标准论点可以单独做你想做的事.
I'm pretty certain there's no par argument that, by itself, does what you want.
不过,这里有一种比您更简单的方法.它使用 plot.default()
的 panel.first
参数在后台绘制一个以原点为中心的巨大填充点.点的范围在每个方向至少是一个 googol 立方单位,我已经确认这适用于 R 的本机 Windows 绘图设备以及 pdf 和 png 设备:它似乎是一个非常通用的解决方案.
Here though is a simpler approach than yours. It uses plot.default()
's panel.first
argument to plot, in the background, a single huge filled point centered at the origin. The point's extent is at least a googol cubed units in every direction, and I've confirmed that this works with R's native Windows plot device, and with pdf and png devices: it appears to be a pretty general solution.
## Your example
plot(x=1, y=2,
panel.first={points(0, 0, pch=16, cex=1e6, col="grey90")
grid(col="white", lty=1)})
## Or, for repeated use, make it a function:
ggbg <- function() {
points(0, 0, pch=16, cex=1e6, col="grey90")
grid(col="white", lty=1)
}
plot(x=1, y=2, panel.first=ggbg())
## Also works for plots running out to 1e300 in each direction
plot(x=-1e300, y=1e300, panel.first=ggbg())
这篇关于绘图区域颜色是否有“par()"设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!