如何将不同的颜色应用于点和文本,但在同一组中?

发布于 2025-02-11 14:40:19 字数 511 浏览 2 评论 0原文

我正在尝试绘制一个剧情,其中这些点的颜色是红色,绿色,蓝色的3组和相关标签的颜色为深色,深色,深色蓝色。

我尝试了以下代码,但没有使用深色。

library(ggplot2)
ggplot(data.frame("Year" = 2013:2022, 
                  "Value" = round(rnorm(10), 1), 
                  "Group" = sample(letters[1:3], 10, T)),
       aes(Year, Value)) + 
  geom_point(aes(color = Group)) +
  geom_text(aes(label = Value, color = Group), vjust = -1, show.legend = FALSE) +
  scale_color_manual(values = c("red", "green", "blue", "darkred", "darkgreen", "darkblue"))

I am trying to draw a plot in which the colors of the points are red, green, blue for 3 groups and colors of the associated labels are darkred, darkgreen, darkblue.

I tried the following code, but the dark colors are not used.

library(ggplot2)
ggplot(data.frame("Year" = 2013:2022, 
                  "Value" = round(rnorm(10), 1), 
                  "Group" = sample(letters[1:3], 10, T)),
       aes(Year, Value)) + 
  geom_point(aes(color = Group)) +
  geom_text(aes(label = Value, color = Group), vjust = -1, show.legend = FALSE) +
  scale_color_manual(values = c("red", "green", "blue", "darkred", "darkgreen", "darkblue"))

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

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

发布评论

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

评论(2

紫﹏色ふ单纯 2025-02-18 14:40:19

我找到了似乎可以做您想要的ggnewscale软件包。
https://ggplot2.tidyverse.org/articles.org/articles/faq-customicles/faq-customising..htmis.htmlis..htmll#颜色

library(ggplot2)
library(ggnewscale)
#> Warning: package 'ggnewscale' was built under R version 4.1.3
ggplot(data.frame("Year" = 2013:2022, 
                  "Value" = round(rnorm(10), 1), 
                  "Group" = sample(letters[1:3], 10, T)),
       aes(Year, Value)) + 
  geom_point(aes(color = Group)) +
  scale_color_manual(values = c("red", "green", "blue")) +
  new_scale_color() + 
  geom_text(aes(label = Value, color = Group), vjust = -1, show.legend = FALSE) +
  scale_color_manual(values = c("darkred", "darkgreen", "darkblue"))

“”

在2022-06-29创建的 reprex软件包(v2.0.1)

I found the ggnewscale package that seems to do what you want.
https://ggplot2.tidyverse.org/articles/faq-customising.html#colours

library(ggplot2)
library(ggnewscale)
#> Warning: package 'ggnewscale' was built under R version 4.1.3
ggplot(data.frame("Year" = 2013:2022, 
                  "Value" = round(rnorm(10), 1), 
                  "Group" = sample(letters[1:3], 10, T)),
       aes(Year, Value)) + 
  geom_point(aes(color = Group)) +
  scale_color_manual(values = c("red", "green", "blue")) +
  new_scale_color() + 
  geom_text(aes(label = Value, color = Group), vjust = -1, show.legend = FALSE) +
  scale_color_manual(values = c("darkred", "darkgreen", "darkblue"))

Created on 2022-06-29 by the reprex package (v2.0.1)

夏花。依旧 2025-02-18 14:40:19

我们可以使用填充而不是颜色作为点并将形状设置为21。如果您不希望要点周围的边框,请添加stroke = NaGEOM_POINT

library(ggplot2)
ggplot(data.frame("Year" = 2013:2022, 
                  "Value" = round(rnorm(10), 1), 
                  "Group" = sample(letters[1:3], 10, T)),
       aes(Year, Value)) + 
  geom_point(aes(fill = Group), pch=21) +
  geom_text(aes(label = Value, color = Group), vjust = -1, 
            show.legend = FALSE) +
  scale_color_manual(values = c("darkred", "darkgreen", "darkblue")) +
  scale_fill_manual(values = c("red", "green", "blue"))

“”

We can use fill instead of color for points and set the shape to 21. If you don't want the border around the points, add stroke = NA to geom_point.

library(ggplot2)
ggplot(data.frame("Year" = 2013:2022, 
                  "Value" = round(rnorm(10), 1), 
                  "Group" = sample(letters[1:3], 10, T)),
       aes(Year, Value)) + 
  geom_point(aes(fill = Group), pch=21) +
  geom_text(aes(label = Value, color = Group), vjust = -1, 
            show.legend = FALSE) +
  scale_color_manual(values = c("darkred", "darkgreen", "darkblue")) +
  scale_fill_manual(values = c("red", "green", "blue"))

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