ggplot2 merge color and fill legends

前端 未结 1 675
春和景丽
春和景丽 2021-01-13 18:51

I want to merge two legends in ggplot2. I use the following code:

ggplot(dat_ribbon, aes(x = x)) +
  geom_ribbon(aes(ymin = ymin, ymax = ymax,
                       


        
1条回答
  •  无人及你
    2021-01-13 19:21

    You are not using ggplot2 according to its philosophy. That makes things difficult.

    ggplot(dat_ribbon, aes(x = x)) +
      geom_ribbon(aes(ymin = ymin, ymax = ymax, group = group, fill = "test4 test5"), 
                  alpha = 0.2) +
      geom_line(aes(y = y, color = "Test2"), data = dat_m) +
      geom_blank(data = data.frame(x = rep(5, 4), y = 0.5, 
                                   group = c("test4 test5", "Test2", "test", "Test3")), 
                 aes(y = y, color = group, fill = group)) +
      scale_color_manual(name = "combined legend",
                         values=c("test4 test5"= NA, "Test2" = "white", 
                                  "test"="black", "Test3"="red")) + 
      scale_fill_manual(name = "combined legend",
                        values = c("test4 test5"= "dodgerblue4", 
                                   "Test2" = NA, "test"=NA, "Test3"=NA)) 
    

    resulting plot

    0 讨论(0)
提交回复
热议问题