更改 R 图的背景颜色
好吧,假设我有以下情节。
df = data.frame(date=c(rep(2008:2013, by=1)),
value=c(303,407,538,696,881,1094))
barplot(df$value, main="TITLE", col="gray", ylab="People", xlab="Years")
如何将背景更改为海军蓝色?
我知道 ggplot2 可以做到这一点,但不确定是否可以使用基本图形来做到这一点。
All right, let's say I have the following plot.
df = data.frame(date=c(rep(2008:2013, by=1)),
value=c(303,407,538,696,881,1094))
barplot(df$value, main="TITLE", col="gray", ylab="People", xlab="Years")
How can I change the background to navy blue?
I know this is possible with ggplot2, but not sure if I can do this with base graphics.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
喜欢
Like
稍后通过 Google 搜索我们得知您可以按照欧文的指示设置整个绘图设备的背景颜色。如果您只想更改绘图区域,则必须执行类似于 R-Help 线程中概述的操作:
barplot
函数有一个add
参数,您可以使用该参数可能需要使用。One Google search later we've learned that you can set the entire plotting device background color as Owen indicates. If you just want the plotting region altered, you have to do something like what is outlined in that R-Help thread:
The
barplot
function has anadd
parameter that you'll likely need to use.老问题,但我有更好的方法来做到这一点。使用
polygon
,而不是使用rect()
。这允许您将所有内容保留在绘图中,而无需使用点。而且你根本不必搞乱par
。如果您想让事情保持自动化,请将多边形
的坐标作为数据的函数。Old question but I have a much better way of doing this. Rather than using
rect()
usepolygon
. This allows you to keep everything inplot
without usingpoints
. Also you don't have to mess withpar
at all. If you want to keep things automated make the coordinates ofpolygon
a function of your data.我使用
abline()
和极宽的垂直线来填充绘图空间:abline(v = xpoints, col = "grey90", lwd = 80)
你必须创建框架,然后是 ablines,然后绘制点,以便它们在顶部可见。如果需要,您甚至可以使用第二个
abline()
语句在灰色上放置细白线或黑线。例子:
I use
abline()
with extremely wide vertical lines to fill the plot space:abline(v = xpoints, col = "grey90", lwd = 80)
You have to create the frame, then the ablines, and then plot the points so they are visible on top. You can even use a second
abline()
statement to put thin white or black lines over the grey, if desired.Example:
将此线程中的信息与 R-help ?rect 结合起来后,我想出了这个漂亮的昼夜节律数据图表 (24 小时图)。背景矩形的脚本是这样的:
根脚本:
我的脚本:
这个想法是用 8 小时光照和 16 小时黑暗来表示 24 小时的日子。
干杯,
罗马里奥
After combining the information in this thread with the R-help ?rect, I came up with this nice graph for circadian rhythm data (24h plot). The script for the background rectangles is this:
root script:
My script:
The idea is to represent days of 24 hours with 8 h light and 16 h dark.
Cheers,
Romário
上述函数提供了与输入颜色的透明版本相对应的颜色代码(在本例中,输入颜色为“blanchedalmond”)。
输入 Alpha 值的范围为 0 到 1,0 表示完全透明,1 表示完全不透明。 (在本例中,alpha 值为 0.3 的“blanchedalmond”半透明鲥鱼的代码是“
#FFEBCD4D
”。请务必包含主题标签符号)。您可以使用本线程前面 joran 提供的此功能将新的半透明颜色设置为背景颜色:通过使用半透明颜色,您可以确保应用背景颜色后仍然可以在下面看到图形的数据。
希望这有帮助!
The above function provides a color code which corresponds to a transparent version of the input color (In this case the input color is "blanchedalmond.").
Input alpha values range on a scale of 0 to 1, 0 being completely transparent and 1 being completely opaque. (In this case, the code for the translucent shad of "blanchedalmond" given an alpha of .3 is "
#FFEBCD4D
." Be sure to include the hashtag symbol). You can make the new translucent color into the background color by using this function provided by joran earlier in this thread:By using a translucent color, you can be sure that the graph's data can still be seen underneath after the background color is applied.
Hope this helps!