r - 如何更改ggplot中图例的样式,使每个元素周围都有一个矩形?
问题描述
这是我得到的图表:
该图是以下代码的结果:
sent_results = structure(list(Date = structure(c(11688, 11719, 11747, 11778,
11808, 11839, 11869, 11931, 11961, 11992, 12022, 12053, 12084,
12112, 12143, 12173, 12204, 12234, 12296, 12326, 12357, 12387,
12418, 12449, 12478, 12509, 12539, 12570, 12600, 12662), class = "Date"),
IP = c(-0.595925341785417, -0.566320864529598, -0.487375591847412,
-0.507111910017959, -0.467639273676865, -0.418298478250498,
-0.497243750932684, -0.428166637335771, -0.487375591847412,
-0.428166637335771, -0.566320864529598, -0.418298478250498,
-0.398562160079952, -0.477507432762138, -0.438034796421045,
-0.595925341785417, -0.595925341785417, -0.438034796421045,
-0.566320864529598, -0.329485046483038, -0.359089523738859,
-0.329485046483038, -0.349221364653585, -0.309748728312493,
-0.329485046483038, -0.250539773800852, -0.230803455630306,
-0.230803455630306, -0.15185818294812, -0.21106713745976),
Sentiment = c(0.010752688172043, 0.0625, 0.0634920634920635,
0.0467289719626168, 0.0761904761904762, 0.0747663551401869,
0.0759493670886076, 0.0306748466257669, -0.025, 0.0136986301369863,
-0.021978021978022, -0.0645161290322581, -0.00746268656716418,
0.0172413793103448, -0.0857142857142857, -0.0229007633587786,
0.0111111111111111, -0.00892857142857143, 0.0126582278481013,
0.0144927536231884, 0.112, 0.0775862068965517, 0.065359477124183,
0.0786516853932584, 0.032967032967033, 0.0378151260504202,
0.0535714285714286, 0.101694915254237, 0.0967741935483871,
0.0892857142857143)), row.names = c(NA, 30L), class = "data.frame")
colors <- c("Economic Outlook Index" = "#4C74C9", "Industrial Production" = "red4")
correction_factor <- 10
pretty_breaks1 <- pretty(c(sent_results$Sentiment, sent_results$IP / correction_factor))
pretty_breaks2 <- pretty_breaks1 * correction_factor
scaled_labels1 <- pretty_breaks1 * 10 # make the tranformation you want to have
scaled_labels2 <- pretty_breaks2 * 10 + 95 # make the tranformation you want to have
ggplot(sent_results) +
geom_line(aes(x= Date, y = Sentiment, colour= "Economic Outlook Index"), size=1) +
geom_line(aes(x= Date, y = IP / correction_factor, colour= "Industrial Production"), linetype = "dashed", size=1) +
theme_classic() +
theme(panel.border = element_rect(fill=NA)) +
geom_rect(data=recessions.df, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='grey', alpha=0.2) +
scale_x_date(date_breaks = "2 years" , date_labels = "%Y") +
labs(y="", x = "", color = "") +
scale_y_continuous(breaks = pretty_breaks1,
labels = scaled_labels1,
sec.axis = sec_axis(trans = ~.*correction_factor,
breaks = pretty_breaks2,
labels = scaled_labels2)) +
scale_color_manual(values = colors) +
theme(legend.position="bottom", legend.margin=margin(-13, 0, 0, 0))
然而,传说不适合我。相反,我想以某种方式更改图例,对于每个图例行和名称,矩形的边界包含我的图例的元素。我想看到它比说它容易。这是一张我想得到传说的照片:
任何人都可以帮我得到它吗?
谢谢!
解决方案
这可能接近您想要的:
library(ggplot2)
#Plot
ggplot(sent_results) +
geom_line(aes(x= Date, y = Sentiment, colour= "Economic Outlook Index"), size=1) +
geom_line(aes(x= Date, y = IP / correction_factor, colour= "Industrial Production"), linetype = "dashed", size=1) +
theme_classic() +
theme(panel.border = element_rect(fill=NA)) +
#geom_rect(data=recessions.df, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='grey', alpha=0.2) +
scale_x_date(date_breaks = "2 years" , date_labels = "%Y") +
labs(y="", x = "", color = "") +
scale_y_continuous(breaks = pretty_breaks1,
labels = scaled_labels1,
sec.axis = sec_axis(trans = ~.*correction_factor,
breaks = pretty_breaks2,
labels = scaled_labels2)) +
scale_color_manual(values = colors) +
theme(legend.position="bottom",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "black"))
输出:
另一种选择是“装箱”图例中的键:
#Plot 2
ggplot(sent_results) +
geom_line(aes(x= Date, y = Sentiment, colour= "Economic Outlook Index"), size=1) +
geom_line(aes(x= Date, y = IP / correction_factor, colour= "Industrial Production"), linetype = "dashed", size=1) +
theme_classic() +
theme(panel.border = element_rect(fill=NA)) +
#geom_rect(data=recessions.df, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='grey', alpha=0.2) +
scale_x_date(date_breaks = "2 years" , date_labels = "%Y") +
labs(y="", x = "", color = "") +
scale_y_continuous(breaks = pretty_breaks1,
labels = scaled_labels1,
sec.axis = sec_axis(trans = ~.*correction_factor,
breaks = pretty_breaks2,
labels = scaled_labels2)) +
scale_color_manual(values = colors) +
theme(legend.position="bottom",
legend.background = element_blank(),
legend.box.background = element_rect(colour = "black"),
legend.key = element_rect(color = "black"))
输出:
推荐阅读
- javascript - 如何自定义此 HTML 文档脚本以使其循环?
- javascript - 刷新一个jsp/网页
- c# - NGINX 反向代理背后的 ASP.NET MVC
- php - PHP Simple Post - Post 参数为空
- angular - 我可以在本地安装@angular/cli@4.2.2 而不在全局降级最新的@angular/cli@6.2.0 版本吗?
- firebase - Firebase database().ref() 不存在的路径
- xml - 如何让我的机器人按顺序而不是随机提问?
- python - 2d 数组到 3d 图像矩阵
- php - 如何防止 fwrite():对等方重置连接
- javascript - 我应该如何实现我的鼓机应用程序?