在 R 中创建具有与绘图相同的 RGB 颜色值的图例?

发布于 2024-12-12 22:06:24 字数 652 浏览 0 评论 0原文

我在 R 中创建了一个具有特定 RGB 颜色值的简单散点图,如下所示:

plot(shuffled, p_val, pch=19, col="black", xlim=c(0,100), ylim=c(0,1))
points(ri, p_val, pch=19, col=rgb(96,123,139, 50, maxColorValue=255), 
       xlim=c(0,100), ylim=c(0,1))
points(somo, p_val, pch=19, col=rgb(225,64,5, 50, maxColorValue=255), 
       xlim=c(0,100), ylim=c(0,1))

我想在上面的代码中使用相同的颜色值来生成图形图例。我使用的代码看起来像这样,但我似乎无法弄清楚如何匹配图点的颜色。

legend("topright", c("Shuffled", "Riffled", "Somosome"), cex=1.0, bty="n", 
       c("black",col=rgb(96,123,139, 50, maxColorValue=255),col=rgb(225,64,5, 50, maxColorValue=255))

有人可以帮忙吗?我的传说有什么问题吗?谢谢!

I've created a simple scatter plot in R with specific RGB color values like this:

plot(shuffled, p_val, pch=19, col="black", xlim=c(0,100), ylim=c(0,1))
points(ri, p_val, pch=19, col=rgb(96,123,139, 50, maxColorValue=255), 
       xlim=c(0,100), ylim=c(0,1))
points(somo, p_val, pch=19, col=rgb(225,64,5, 50, maxColorValue=255), 
       xlim=c(0,100), ylim=c(0,1))

I would like to use the same color values in the code above to generate a figure legend. The code I am using looks like this, but I can't seem to figure out how to match the colors of the graph points.

legend("topright", c("Shuffled", "Riffled", "Somosome"), cex=1.0, bty="n", 
       c("black",col=rgb(96,123,139, 50, maxColorValue=255),col=rgb(225,64,5, 50, maxColorValue=255))

Can any one help? What is wrong with my legend? Thanks!

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

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

发布评论

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

评论(2

洋洋洒洒 2024-12-19 22:06:27

问题是您将 R 颜色关键字“black”与十六进制颜色键连接起来。像这样的东西应该有效:

colvec <- rgb(rbind(t(col2rgb("black")), ## this is obviously just 0, 0, 0
                    c(96,123,139),
                    c(225,64,5)),
              alpha=50,max=255)

plot(shuffled, p_val, pch=19, col=colvec[1], xlim=c(0,100), ylim=c(0,1))
points(ri, p_val, pch=19, col=colvec[2], 
       xlim=c(0,100), ylim=c(0,1))
points(somo, p_val, pch=19, col=colvec[3], 
       xlim=c(0,100), ylim=c(0,1))
legend("topright", c("Shuffled", "Riffled", "Somosome"), cex=1.0, bty="n",
       col=colvec)

The problem is that you are concatenating an R color keyword, "black", with hexadecimal color keys. Something like this should work:

colvec <- rgb(rbind(t(col2rgb("black")), ## this is obviously just 0, 0, 0
                    c(96,123,139),
                    c(225,64,5)),
              alpha=50,max=255)

plot(shuffled, p_val, pch=19, col=colvec[1], xlim=c(0,100), ylim=c(0,1))
points(ri, p_val, pch=19, col=colvec[2], 
       xlim=c(0,100), ylim=c(0,1))
points(somo, p_val, pch=19, col=colvec[3], 
       xlim=c(0,100), ylim=c(0,1))
legend("topright", c("Shuffled", "Riffled", "Somosome"), cex=1.0, bty="n",
       col=colvec)
∞梦里开花 2024-12-19 22:06:26

这对我有用:

plot(1:3,
    col=c("black",rgb(96,123,139, 50, maxColorValue=255),
               rgb(225,64,5, 50,maxColorValue=255)),pch=19)

legend("topright", c("Shuffled", "Riffled", "Somosome"), cex=1.0, bty="n",
   col=c("black",rgb(96,123,139, 50, maxColorValue=255),
      rgb(225,64,5, 50, maxColorValue=255)),pch=19)

请注意,您需要指定一个 col= 向量、标签的大小,并且还必须在那里有一个 pch= 。或者,您可以执行 fill=[颜色向量] 来绘制填充框。

This works for me:

plot(1:3,
    col=c("black",rgb(96,123,139, 50, maxColorValue=255),
               rgb(225,64,5, 50,maxColorValue=255)),pch=19)

legend("topright", c("Shuffled", "Riffled", "Somosome"), cex=1.0, bty="n",
   col=c("black",rgb(96,123,139, 50, maxColorValue=255),
      rgb(225,64,5, 50, maxColorValue=255)),pch=19)

Note you need to specify one vector of col=, the size of your labels, and you also have to have a pch= in there too. Alternatively you can do fill=[vector of colours] to draw filled boxes.

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