用每个组的背景绘制

发布于 2025-02-10 14:59:44 字数 1028 浏览 2 评论 0原文

我正在尝试根据一组实例创建一个在背景上具有不同颜色的散点图。这是数据:

data = data.frame(Group= c(rep(1,12),rep(2,12),rep(3,12),rep(4,12),rep(5,10)), Id=1:58, Values=rnorm(58, mean=2300, sd=500))

如您所见,ID列包含1到58之间的所有数字,我想为每个组具有不同的背景颜色。这是我尝试的:

library(RColorBrewer)
library(colorspace)
color = c("#ffffd9","#edf8b1", "#c7e9b4", "#7fcdbb", "#41b6c4", "#225ea8")
color = adjust_transparency(color, alpha = 0.4) #adding transparacy to the colors
plot(data$Id, data$Value)
polygon(x=c(1,1,12,12), y=c(0,3500,3500,0), col=color[1], border=F)
polygon(x=c(13,13,24,24), y=c(0,3500,3500,0), col=color[2], border=F)
polygon(x=c(25,25,36,36), y=c(0,3500,3500,0), col=color[3], border=F)
polygon(x=c(37,37,48,48), y=c(0,3500,3500,0), col=color[4], border=F)
polygon(x=c(49,49,58,58), y=c(0,3500,3500,0), col=color[5], border=F)

结果是以下非常丑陋的情节。我无法在多边形后面得到地块。

“

,如果有人可以建议您建议更好,请告诉我。 最好是,我希望继续使用基本R,而不是GGPLOT2。 非常感谢

I am trying to create a scatterplot having different colors on the background based on the group of instances. This is the data:

data = data.frame(Group= c(rep(1,12),rep(2,12),rep(3,12),rep(4,12),rep(5,10)), Id=1:58, Values=rnorm(58, mean=2300, sd=500))

As you can see, the Id column contains all numbers between 1 and 58, and I would like to have a different background color for each group. This is was I tried:

library(RColorBrewer)
library(colorspace)
color = c("#ffffd9","#edf8b1", "#c7e9b4", "#7fcdbb", "#41b6c4", "#225ea8")
color = adjust_transparency(color, alpha = 0.4) #adding transparacy to the colors
plot(data$Id, data$Value)
polygon(x=c(1,1,12,12), y=c(0,3500,3500,0), col=color[1], border=F)
polygon(x=c(13,13,24,24), y=c(0,3500,3500,0), col=color[2], border=F)
polygon(x=c(25,25,36,36), y=c(0,3500,3500,0), col=color[3], border=F)
polygon(x=c(37,37,48,48), y=c(0,3500,3500,0), col=color[4], border=F)
polygon(x=c(49,49,58,58), y=c(0,3500,3500,0), col=color[5], border=F)

The result is the following very ugly plot. I can't get the plots behind the polygons.

Ugly plot

Also, if anyone has a better palette to suggest, please let me know.
Preferibly, I would prefer to keep using base R and not ggplot2.
Thanks a lot in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浮光之海 2025-02-17 14:59:44

在基本图形中定义颜色有很多可能性。您在代码中加载rcolorbrewer。手动页面表明,根据您想要交流的方式,有3种类型的调色板。例如,顺序调色板是一系列逐渐变化的颜色:

colori <- brewer.pal(5, "Blues")
# Other options: Blues BuGn BuPu GnBu Greens Greys Oranges OrRd PuBu PuBuGn PuRd Purples RdPu Reds YlGn YlGnBu YlOrBr YlOrRd
colori = adjust_transparency(colori, alpha = 0.4)

现在,如果我们使用透明度覆盖符号是可选的,我们可以对您的代码进行修改。我们也可以消除白色条。

plot(data$Id, data$Value, type="n")
polygon(x=c(1,1,12,12), y=c(0,3500,3500,0), col=colori[1], border=F)
polygon(x=c(12,12,24,24), y=c(0,3500,3500,0), col=colori[2], border=F)
polygon(x=c(24,24,36,36), y=c(0,3500,3500,0), col=colori[3], border=F)
polygon(x=c(36,36,48,48), y=c(0,3500,3500,0), col=colori[4], border=F)
polygon(x=c(48,48,59,59), y=c(0,3500,3500,0), col=colori[5], border=F)
points(data$Id, data$Value)

There are many possibilities for defining colors in base graphics. You load RColorBrewer in your code. The manual page indicates that there are 3 types of palettes depending on what you want to communicate. For example a sequential palette is a sequence of gradually changing colors:

colori <- brewer.pal(5, "Blues")
# Other options: Blues BuGn BuPu GnBu Greens Greys Oranges OrRd PuBu PuBuGn PuRd Purples RdPu Reds YlGn YlGnBu YlOrBr YlOrRd
colori = adjust_transparency(colori, alpha = 0.4)

Now we can revise your code a bit if we overprint the symbols using transparency is optional. Also we can eliminate the white bars.

plot(data$Id, data$Value, type="n")
polygon(x=c(1,1,12,12), y=c(0,3500,3500,0), col=colori[1], border=F)
polygon(x=c(12,12,24,24), y=c(0,3500,3500,0), col=colori[2], border=F)
polygon(x=c(24,24,36,36), y=c(0,3500,3500,0), col=colori[3], border=F)
polygon(x=c(36,36,48,48), y=c(0,3500,3500,0), col=colori[4], border=F)
polygon(x=c(48,48,59,59), y=c(0,3500,3500,0), col=colori[5], border=F)
points(data$Id, data$Value)

Alternate Plot

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