ggplot2: Different legend symbols for points and lines

后端 未结 1 1013
无人共我
无人共我 2020-12-08 11:49

already searched all related threads about this but could not find a solution.

Here is my code and the attached plot result:

g <-ggplot(NDVI2, aes         


        
相关标签:
1条回答
  • 2020-12-08 11:54

    override.aes is definitely a good start for customizing the legend. In your case you may remove unwanted shape in the legend by setting them to NA, and set unwanted linetype to blank:

    ggplot(data = NDVI2, aes(x = LAI2, y = NDVI, colour = Legend)) + 
      geom_point(size = 3) +
      geom_smooth(aes(group = 1, colour = "Trendline"),
                   method = "loess", se = FALSE, linetype = "dashed") +
      geom_smooth(aes(group = 1, colour = "Regression (log)"),
                   method = "lm", formula = y ~ log(x), se = FALSE, linetype = "solid") +
      scale_colour_manual(values = c("purple", "green", "blue", "yellow", "magenta","orange", "cyan", "red", "black"),
                           guide = guide_legend(override.aes = list(
                             linetype = c(rep("blank", 7), "solid", "dashed"),
                             shape = c(rep(16, 7), NA, NA))))
    

    enter image description here

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