提问人:george1994 提问时间:4/30/2022 最后编辑:Allan Camerongeorge1994 更新时间:4/30/2022 访问量:502
ggplot2 scale_color_identity() 如何更改图例的线型和设计?
ggplot2 scale_color_identity() how to change linestyle and design of the legend?
问:
我编写了以下 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
提前致谢
答:
1赞
Allan Cameron
4/30/2022
#1
您的代码有很多不必要的重复,并且您没有利用 ggplot 的语法。
图例中出现垂直虚线的原因是,您的一个调用包含颜色映射,因此其绘制键将添加到图例中。您可以将其更改为来解决此问题。请注意,您只需要一次调用,因为您可以有多个 x 截距。geom_vline
key_glyph
draw_key_path
geom_vline
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)
评论