更改 R 图的背景颜色

发布于 2024-12-01 21:47:41 字数 288 浏览 1 评论 0原文

好吧,假设我有以下情节。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

岁月静好 2024-12-08 21:47:41

喜欢

par(bg = 'blue')
# Now do plot

Like

par(bg = 'blue')
# Now do plot
空城仅有旧梦在 2024-12-08 21:47:41

稍后通过 Google 搜索我们得知您可以按照欧文的指示设置整个绘图设备的背景颜色。如果您只想更改绘图区域,则必须执行类似于 R-Help 线程中概述的操作:

plot(df)
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "gray")
points(df)

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:

plot(df)
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "gray")
points(df)

The barplot function has an add parameter that you'll likely need to use.

巾帼英雄 2024-12-08 21:47:41

老问题,但我有更好的方法来做到这一点。使用polygon,而不是使用rect()。这允许您将所有内容保留在绘图中,而无需使用点。而且你根本不必搞乱 par 。如果您想让事情保持自动化,请将多边形的坐标作为数据的函数。

plot.new()
polygon(c(-min(df[,1])^2,-min(df[,1])^2,max(df[,1])^2,max(df[,1])^2),c(-min(df[,2])^2,max(df[,2])^2,max(df[,2])^2,-min(df[,2])^2), col="grey")
par(new=T)
plot(df)

Old question but I have a much better way of doing this. Rather than using rect() use polygon. This allows you to keep everything in plot without using points. Also you don't have to mess with par at all. If you want to keep things automated make the coordinates of polygon a function of your data.

plot.new()
polygon(c(-min(df[,1])^2,-min(df[,1])^2,max(df[,1])^2,max(df[,1])^2),c(-min(df[,2])^2,max(df[,2])^2,max(df[,2])^2,-min(df[,2])^2), col="grey")
par(new=T)
plot(df)
捶死心动 2024-12-08 21:47:41

我使用 abline() 和极宽的垂直线来填充绘图空间:

abline(v = xpoints, col = "grey90", lwd = 80)

你必须创建框架,然后是 ablines,然后绘制点,以便它们在顶部可见。如果需要,您甚至可以使用第二个 abline() 语句在灰色上放置细白线或黑线。

例子:

xpoints = 1:20
y = rnorm(20)
plot(NULL,ylim=c(-3,3),xlim=xpoints)
abline(v=xpoints,col="gray90",lwd=80)
abline(v=xpoints,col="white")
abline(h = 0, lty = 2) 
points(xpoints, y, pch = 16, cex = 1.2, col = "red")

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:

xpoints = 1:20
y = rnorm(20)
plot(NULL,ylim=c(-3,3),xlim=xpoints)
abline(v=xpoints,col="gray90",lwd=80)
abline(v=xpoints,col="white")
abline(h = 0, lty = 2) 
points(xpoints, y, pch = 16, cex = 1.2, col = "red")
窗影残 2024-12-08 21:47:41

将此线程中的信息与 R-help ?rect 结合起来后,我想出了这个漂亮的昼夜节律数据图表 (24 小时图)。背景矩形的脚本是这样的:

根脚本:

>rect(xleft, ybottom, xright, ytop, col = NA, border = NULL)

我的脚本:

>i <- 24*(0:8)
>rect(8+i, 1, 24+i, 130, col = "lightgrey", border=NA)
>rect(8+i, -10, 24+i, 0.1, col = "black", border=NA)

这个想法是用 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:

>rect(xleft, ybottom, xright, ytop, col = NA, border = NULL)

My script:

>i <- 24*(0:8)
>rect(8+i, 1, 24+i, 130, col = "lightgrey", border=NA)
>rect(8+i, -10, 24+i, 0.1, col = "black", border=NA)

The idea is to represent days of 24 hours with 8 h light and 16 h dark.

Cheers,

Romário

Smile简单爱 2024-12-08 21:47:41
adjustcolor("blanchedalmond",alpha.f = 0.3)

上述函数提供了与输入颜色的透明版本相对应的颜色代码(在本例中,输入颜色为“blanchedalmond”)。

输入 Alpha 值的范围为 0 到 1,0 表示完全透明,1 表示完全不透明。 (在本例中,alpha 值为 0.3 的“blanchedalmond”半透明鲥鱼的代码是“#FFEBCD4D”。请务必包含主题标签符号)。您可以使用本线程前面 joran 提供的此功能将新的半透明颜色设置为背景颜色:

rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "blanchedalmond")

通过使用半透明颜色,您可以确保应用背景颜色后仍然可以在下面看到图形的数据。
希望这有帮助!

adjustcolor("blanchedalmond",alpha.f = 0.3)

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:

rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "blanchedalmond")

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文