Combine legend in ggplot2

后端 未结 2 1506
长情又很酷
长情又很酷 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 00:48

    Specify a . as the shape symbol for your curve and a blank line for your points:

    p <- ggplot(df, aes(x=x, y=value)) +
      geom_point(aes(colour=variable, shape=variable, linetype = variable), size = 3) +
      stat_function(aes(colour="log2(x)", shape = "log2(x)", linetype = "log2(x)"), fun=log2) +
      scale_shape_manual(values = setNames(c(16, 17, 46), c("a", "b", "log2(x)"))) +
      scale_linetype_manual(values = setNames(c(0, 0, 1), c("a", "b", "log2(x)")))
    print(p)
    

提交回复
热议问题