用每个组的背景绘制
我正在尝试根据一组实例创建一个在背景上具有不同颜色的散点图。这是数据:
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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在基本图形中定义颜色有很多可能性。您在代码中加载
rcolorbrewer
。手动页面表明,根据您想要交流的方式,有3种类型的调色板。例如,顺序调色板是一系列逐渐变化的颜色:现在,如果我们使用透明度覆盖符号是可选的,我们可以对您的代码进行修改。我们也可以消除白色条。
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:Now we can revise your code a bit if we overprint the symbols using transparency is optional. Also we can eliminate the white bars.