[Solved] How to add a legend to ggplot when aes are constant for stat_smooth


You can still use aes in each stat_smooth including only the desired color. This will add legends to the plot. Then you can use scale_color_identity to match and rename the colors

ggplot(mtcars, aes(x=wt, y = mpg, col="green")) + 
  geom_point(col="blue") +
  stat_smooth(method='loess',linetype="dashed", aes(color = "red"), span=0.1) +
  stat_smooth(method='loess',linetype="dashed", aes(color = "orange"), span=0.25) +
  stat_smooth(method='loess',linetype="dashed", aes(color = "yellow"), span=0.5) +
  stat_smooth(method='loess',linetype="dashed", aes(color = "green"), span=0.75) +
  labs(title = "Fitting Price ~ living space, span=0.1, 0.25, 0.5, 0.75") +
  scale_color_identity(name = "Span values",
                          breaks = c("red", "orange", "yellow", "green"),
                          labels = c("0.1", "0.25", "0.5", "0.75"),
                          guide = "legend")

Read more at Creating legends when aesthetics are constants in ggplot2

solved How to add a legend to ggplot when aes are constant for stat_smooth