问题
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