R 如何更改ggplot中图例的样式,使每个元素周围都有一个矩形?

R 如何更改ggplot中图例的样式,使每个元素周围都有一个矩形?,r,ggplot2,R,Ggplot2,这是我得到的图表: 此图是以下代码的结果: 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,

这是我得到的图表:

此图是以下代码的结果:

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))

发送的结果=结构(列表日期=结构, 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=“日期”), 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), 情绪=c(0.010752688172043,0.0625,0.06349206344920635,, 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”)
颜色这可以接近您想要的颜色:

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"))
输出:


下一次,我鼓励您制作一个最小的可复制示例-不要使用196行,而10行或20行就可以了。