How can I colour variables in a ggplot without creating multiple regression lines?

前端 未结 1 1074
借酒劲吻你
借酒劲吻你 2021-01-27 13:28

I\'m creating a ggplot and I want to colour data points by the transect they came from. However when I do this using the colour=transect argument I end up with a re

相关标签:
1条回答
  • 2021-01-27 14:23

    This could be achieved by making color a local asthetic of the geom_point layer:

    library(ggplot2)
    
    set.seed(42)
    leaf.data <- data.frame(
      distance.from.ecotone..m. = runif(30, 0, 30),
      mean.herbivory.... = runif(30, -5, 15),
      transect = factor(sample(1:5, 30, replace = TRUE))
    )
    
    ggplot(data=leaf.data, aes(x=distance.from.ecotone..m., y=mean.herbivory....)) +
      geom_point(aes(colour=transect)) +
      geom_smooth(method = "lm", na.rm = TRUE, fullrange= TRUE)+
      labs(x="Distance from Ecotone (m)", y="Mean Herbivory per Tree (%)",
           title="Herbivory as a Function of Distance from an Ecotone")
    #> `geom_smooth()` using formula 'y ~ x'
    

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