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

北城以北 提交于 2021-02-05 12:13:37

问题


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 regression line for each transect as well:

Here is my code:

ggplot(data=leaf.data, 
       aes(x=distance.from.ecotone..m., y=mean.herbivory....,colour=transect)) +
  geom_point() +
  geom_smooth(method = "lm", na.rm = TRUE, fullrange= TRUE, aes=(group=1))+
  labs(x="Distance from Ecotone (m)", y="Mean Herbivory per Tree (%)",
       title="Herbivory as a Function of Distance from an Ecotone")

回答1:


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'



来源:https://stackoverflow.com/questions/64368813/how-can-i-colour-variables-in-a-ggplot-without-creating-multiple-regression-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!