ggplot2 scale_color_identity()如何更改传奇的线性和设计?

发布于 2025-01-25 05:26:35 字数 1939 浏览 1 评论 0 原文

我对以下GGPLOT进行了编码。问题在于传说的设计存在缺陷:传说的元素与似乎虚线的线条相互联系。如何将其删除?此外,海军应该是一条虚线,但显示为坚实的线路。有可能改变吗?这是我的代码:

plot1 <- ggplot() +
  geom_line(aes(x = datacom$Datum , y = datacom$`CDU/CSU`, colour = "black"),size=0.8) +
  geom_line(aes(x = datacom$Datum , y = datacom$SPD, colour = "red"),size=0.8) +
  geom_line(aes(x = datacom$Datum , y = datacom$GRÜNE, col = "green"),size=0.8) +
  geom_line(aes(x = datacom$Datum , y = datacom$FDP, col = "gold1"),size=0.8) +
  geom_line(aes(x = datacom$Datum , y = datacom$`Linke/PDS`, col = "darkred"),size=0.8) +
  geom_line(aes(x = datacom$Datum[154:168] , y = datacom$Piraten[154:168], col = "tan1"),size=0.8) +
  geom_line(aes(x = datacom$Datum[169:272] , y = datacom$AfD[169:272], col = "blue"),size=0.8) +
  geom_line(aes(x = datacom$Datum , y = datacom$Sonstige, col = "grey"),size=0.8) +
  geom_vline(aes(xintercept = datacom$Datum[263], color = "navy"), linetype="longdash",size = 0.5)+
  geom_vline(xintercept = datacom$Datum[215], color = "navy", size = 0.5,linetype="longdash")+
  geom_vline(xintercept = datacom$Datum[167], color = "navy", size = 0.5,linetype="longdash")+
  geom_vline(xintercept = datacom$Datum[127], color = "navy", size = 0.5,linetype="longdash")+
  geom_vline(xintercept = datacom$Datum[79], color = "navy", size = 0.5,linetype="longdash")+
  geom_vline(xintercept = datacom$Datum[44], color = "navy", size = 0.5,linetype="longdash")+
  scale_color_identity(name = NULL, labels = c(black = "CDU/CSU", red = "SPD",green="Die Grünen",gold1="FDP",darkred = "Die Linke/PDS",tan1="Piraten",blue="AfD",grey="Sonstige",navy="Bundestagswahlen"), guide = "legend") +
   theme_bw() +
  theme(legend.position = "bottom") +
  theme(axis.text.x = element_text(angle = 90)) +
  labs(title="Forsa-Sonntagsfrage Bundestagswahl in %")+ylab("Prozent")+xlab("Jahre")

plot1

预先感谢

“

I coded the following ggplot. The problem is that the design of the legend is flawed: The elements of the legend are interconnected with what appears to be dashed lines. How can this be removed? And furthermore, navy should be a dashed line, but it is shown as a solid one. Is there a possibility to change that? This is my code:

plot1 <- ggplot() +
  geom_line(aes(x = datacom$Datum , y = datacom

I coded the following ggplot. The problem is that the design of the legend is flawed: The elements of the legend are interconnected with what appears to be dashed lines. How can this be removed? And furthermore, navy should be a dashed line, but it is shown as a solid one. Is there a possibility to change that? This is my code:

CDU/CSU`, colour = "black"),size=0.8) + geom_line(aes(x = datacom$Datum , y = datacom$SPD, colour = "red"),size=0.8) + geom_line(aes(x = datacom$Datum , y = datacom$GRÜNE, col = "green"),size=0.8) + geom_line(aes(x = datacom$Datum , y = datacom$FDP, col = "gold1"),size=0.8) + geom_line(aes(x = datacom$Datum , y = datacom

I coded the following ggplot. The problem is that the design of the legend is flawed: The elements of the legend are interconnected with what appears to be dashed lines. How can this be removed? And furthermore, navy should be a dashed line, but it is shown as a solid one. Is there a possibility to change that? This is my code:

Linke/PDS`, col = "darkred"),size=0.8) + geom_line(aes(x = datacom$Datum[154:168] , y = datacom$Piraten[154:168], col = "tan1"),size=0.8) + geom_line(aes(x = datacom$Datum[169:272] , y = datacom$AfD[169:272], col = "blue"),size=0.8) + geom_line(aes(x = datacom$Datum , y = datacom$Sonstige, col = "grey"),size=0.8) + geom_vline(aes(xintercept = datacom$Datum[263], color = "navy"), linetype="longdash",size = 0.5)+ geom_vline(xintercept = datacom$Datum[215], color = "navy", size = 0.5,linetype="longdash")+ geom_vline(xintercept = datacom$Datum[167], color = "navy", size = 0.5,linetype="longdash")+ geom_vline(xintercept = datacom$Datum[127], color = "navy", size = 0.5,linetype="longdash")+ geom_vline(xintercept = datacom$Datum[79], color = "navy", size = 0.5,linetype="longdash")+ geom_vline(xintercept = datacom$Datum[44], color = "navy", size = 0.5,linetype="longdash")+ scale_color_identity(name = NULL, labels = c(black = "CDU/CSU", red = "SPD",green="Die Grünen",gold1="FDP",darkred = "Die Linke/PDS",tan1="Piraten",blue="AfD",grey="Sonstige",navy="Bundestagswahlen"), guide = "legend") + theme_bw() + theme(legend.position = "bottom") + theme(axis.text.x = element_text(angle = 90)) + labs(title="Forsa-Sonntagsfrage Bundestagswahl in %")+ylab("Prozent")+xlab("Jahre") plot1

Thanks in advance

The Graphic:

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

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

发布评论

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

评论(1

无声情话 2025-02-01 05:26:35

您的代码具有很多不必要的重复,您没有利用GGPLOT的语法。

传说中垂直虚线的原因是,您的 geom_vline 调用之一包括颜色映射,因此其绘制键正在添加到图例中。您可以将其 key_glyph 更改为 draw_key_path 来解决此问题。请注意,您只需要一个 geom_vline 调用,因为您可以具有多个X截距。

ggplot(datacom, aes(x = Datum)) +
  geom_line(aes(y = `CDU/CSU`, colour = "black"), size = 0.8) +
  geom_line(aes(y = SPD, colour = "red"), size = 0.8) +
  geom_line(aes(y = GRÜNE, col = "green"), size = 0.8) +
  geom_line(aes(y = FDP, col = "gold1"), size = 0.8) +
  geom_line(aes(y = `Linke/PDS`, col = "darkred"),size = 0.8) +
  geom_line(aes(y = Piraten, col = "tan1"), 
            data = datacom[154:168,], size = 0.8) +
  geom_line(aes(y = AfD, col = "blue"),
            data = datacom[169:272,], size = 0.8) +
  geom_line(aes(y = Sonstige, col = "grey"), size = 0.8) +
  geom_vline(data = datacom[c(263, 215, 167, 127, 79, 44),],
             aes(xintercept = Datum, color = "navy"), linetype = "longdash", 
             size = 0.5, key_glyph = draw_key_path)+
  scale_color_identity(name = NULL, 
                       labels = c(black = "CDU/CSU", red = "SPD",
                                  green = "Die Grünen", gold1 = "FDP",
                                  darkred = "Die Linke/PDS",
                                  tan1 = "Piraten", blue = "AfD",
                                  grey = "Sonstige",
                                  navy = "Bundestagswahlen"),
                       guide = "legend") +
  theme_bw() +
  theme(legend.position = "bottom",
        axis.text.x     = element_text(angle = 90)) +
  labs(title = "Forsa-Sonntagsfrage Bundestagswahl in %",
       y     = "Prozent",
       x     = "Jahre")

使您的情节的更好方法是将数据转移到长时间的格式中。这仅意味着只有一个 geom_line 呼叫:

library(tidyverse)

datacom %>%
  mutate(Piraten = c(rep(NA, 153), Piraten[154:168], 
                     rep(NA, nrow(datacom) - 168)),
         AfD     = c(rep(NA, 168), AfD[169:272], 
                     rep(NA, nrow(datacom) - 272))) %>%
  pivot_longer(-Datum, names_to = "Series") %>%
  ggplot(aes(x = Datum, y = value, color = Series)) +
  geom_line(size = 0.8, na.rm = TRUE) +
  geom_vline(data = datacom[c(263, 215, 167, 127, 79, 44),],
             aes(xintercept = Datum, color = "Bundestagswahlen"), 
             linetype = "longdash", size = 0.5, key_glyph = draw_key_path) +
  scale_color_manual(name = NULL, 
                     values = c("CDU/CSU" = "black", SPD = "red",
                                "GRÜNE" = "green", FDP = "gold1",
                                "Linke/PDS" = "darkred",
                                Piraten = "tan1", AfD = "blue",
                                Sonstige = "grey",
                                "Bundestagswahlen" = "navy")) +
  theme_bw() +
  theme(legend.position = "bottom",
        axis.text.x     = element_text(angle = 90)) +
  labs(title = "Forsa-Sonntagsfrage Bundestagswahl in %",
       y     = "Prozent",
       x     = "Jahre")

“在此处输入图像说明”


用于创建绘图的数据

显然,我必须创建一些数据才能使您的代码运行,因为您没有提供任何。这是我创建数据的代码

var <- seq(5, 15, length = 280)

datacom <- data.frame(Datum       = seq(as.POSIXct("1999-01-01"), 
                                        as.POSIXct("2022-04-01"), by = "month"),
                      `CDU/CSU`   = 40 + cumsum(rnorm(280)),
                      SPD         = 40 + cumsum(rnorm(280)),
                      GRÜNE       = rpois(280, var),
                      FDP         = rpois(280, var),
                      `Linke/PDS` = rpois(280, var),
                      Piraten     = rpois(280, var),
                      AfD         = rpois(280, var),
                      Sonstige    = rpois(280, var), check.names = FALSE)

Your code has a lot of unnecessary repetition and you are not taking advantage of the syntax of ggplot.

The reason for the vertical dashed lines in the legend is that one of your geom_vline calls includes a color mapping, so its draw key is being added to the legend. You can change its key_glyph to draw_key_path to fix this. Note that you only need a single geom_vline call, since you can have multiple x intercepts.

ggplot(datacom, aes(x = Datum)) +
  geom_line(aes(y = `CDU/CSU`, colour = "black"), size = 0.8) +
  geom_line(aes(y = SPD, colour = "red"), size = 0.8) +
  geom_line(aes(y = GRÜNE, col = "green"), size = 0.8) +
  geom_line(aes(y = FDP, col = "gold1"), size = 0.8) +
  geom_line(aes(y = `Linke/PDS`, col = "darkred"),size = 0.8) +
  geom_line(aes(y = Piraten, col = "tan1"), 
            data = datacom[154:168,], size = 0.8) +
  geom_line(aes(y = AfD, col = "blue"),
            data = datacom[169:272,], size = 0.8) +
  geom_line(aes(y = Sonstige, col = "grey"), size = 0.8) +
  geom_vline(data = datacom[c(263, 215, 167, 127, 79, 44),],
             aes(xintercept = Datum, color = "navy"), linetype = "longdash", 
             size = 0.5, key_glyph = draw_key_path)+
  scale_color_identity(name = NULL, 
                       labels = c(black = "CDU/CSU", red = "SPD",
                                  green = "Die Grünen", gold1 = "FDP",
                                  darkred = "Die Linke/PDS",
                                  tan1 = "Piraten", blue = "AfD",
                                  grey = "Sonstige",
                                  navy = "Bundestagswahlen"),
                       guide = "legend") +
  theme_bw() +
  theme(legend.position = "bottom",
        axis.text.x     = element_text(angle = 90)) +
  labs(title = "Forsa-Sonntagsfrage Bundestagswahl in %",
       y     = "Prozent",
       x     = "Jahre")

enter image description here

An even better way to make your plot would be to pivot the data into long format. This would mean only a single geom_line call:

library(tidyverse)

datacom %>%
  mutate(Piraten = c(rep(NA, 153), Piraten[154:168], 
                     rep(NA, nrow(datacom) - 168)),
         AfD     = c(rep(NA, 168), AfD[169:272], 
                     rep(NA, nrow(datacom) - 272))) %>%
  pivot_longer(-Datum, names_to = "Series") %>%
  ggplot(aes(x = Datum, y = value, color = Series)) +
  geom_line(size = 0.8, na.rm = TRUE) +
  geom_vline(data = datacom[c(263, 215, 167, 127, 79, 44),],
             aes(xintercept = Datum, color = "Bundestagswahlen"), 
             linetype = "longdash", size = 0.5, key_glyph = draw_key_path) +
  scale_color_manual(name = NULL, 
                     values = c("CDU/CSU" = "black", SPD = "red",
                                "GRÜNE" = "green", FDP = "gold1",
                                "Linke/PDS" = "darkred",
                                Piraten = "tan1", AfD = "blue",
                                Sonstige = "grey",
                                "Bundestagswahlen" = "navy")) +
  theme_bw() +
  theme(legend.position = "bottom",
        axis.text.x     = element_text(angle = 90)) +
  labs(title = "Forsa-Sonntagsfrage Bundestagswahl in %",
       y     = "Prozent",
       x     = "Jahre")

enter image description here


Data used to create plot

Obviously, I had to create some data to get your code to run, since you didn't supply any. Here is my code for creating the data

var <- seq(5, 15, length = 280)

datacom <- data.frame(Datum       = seq(as.POSIXct("1999-01-01"), 
                                        as.POSIXct("2022-04-01"), by = "month"),
                      `CDU/CSU`   = 40 + cumsum(rnorm(280)),
                      SPD         = 40 + cumsum(rnorm(280)),
                      GRÜNE       = rpois(280, var),
                      FDP         = rpois(280, var),
                      `Linke/PDS` = rpois(280, var),
                      Piraten     = rpois(280, var),
                      AfD         = rpois(280, var),
                      Sonstige    = rpois(280, var), check.names = FALSE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文