Removing the border of legend symbol

前端 未结 2 1740
死守一世寂寞
死守一世寂寞 2020-11-28 15:24

I was trying to plot some predicted vs. actual data, something that resembles the following:

# Some random data
x <- seq(1: 10)
y_pred <- runif(10, min         


        
2条回答
  •  执笔经年
    2020-11-28 15:51

    A better way to address this question would be to specify show.legend = F option in the geom_ribbon(). This will eliminate the need for the second step for adding and merging the legend key for the confidence interval. Here is the code with slight modifications.

      ggplot(my_dff, aes(x = x, y = y_pred)) +
        geom_line(aes(colour = "Forecasted Data"), size = 1) +
        geom_point(aes(x = x, y = y_obs, colour = "Actual Data"), size = 1) +
        geom_ribbon(aes(ymin=Lo.95, ymax=Hi.95, x=x, linetype = NA,  colour = "Confidence Interval"), alpha=0.2, show.legend = F) +
        theme_grey() +
        scale_colour_manual(
          values = c("blue", "gray30", "red"))+
          guides(color = guide_legend(
            override.aes = list(linetype = c(1, 1, 0)), 
            shape = c(1, NA, NA),
            reverse = T))
    

    My plot

    Credit to https://stackoverflow.com/users/4282026/marblo for their answer to similar question.

提交回复
热议问题