Combine legend in ggplot2

后端 未结 2 1493
长情又很酷
长情又很酷 2021-01-01 00:08

I have a plot of multiple geom_point and a single stat_function in ggplot2. Is there a way to show a single legend?

df         


        
2条回答
  •  醉梦人生
    2021-01-01 01:01

    Probably an easier alternative is to use override.aes as follows:

    ggplot(df, aes(x = x, y = value)) +
      geom_point(aes(colour = variable, shape = variable), size = 3) +
      stat_function(aes(colour = "log2(x)"), fun = log2, size = 1.5) +
      guides(shape = FALSE,
             colour = guide_legend(override.aes = list(shape = c(16, 17, NA),
                                                       linetype = c("blank", "blank", "solid"))))
    

    which results in:

提交回复
热议问题