I am trying to add a line through my plotted data using geom_smooth, but I am running into difficulty.
Here is my code:
plot.BG = ggplot(da
Aesthetic mappings defined in ggplot() are inherited by subsequent layers. However, if mappings defined in a layer (e.g., inside geom_point()) are local to that layer only. Since you want the same mappings to be used by both the geom_point and the geom_smooth layers, put them in the initial ggplot() call and they will be inherited by both.
Reproducibly using mtcars:
# only the points are displayed
ggplot(mtcars) +
geom_point(aes(x = hp, y = mpg, color = factor(cyl)) +
geom_smooth()
# you could respecify for the geom smooth, but that's repetitive
ggplot(mtcars) +
geom_point(aes(x = hp, y = mpg, color = factor(cyl)) +
geom_smooth(aes(x = hp, y = mpg, color = factor(cyl))
# put the mapping up front for all layers to inherit it
ggplot(mtcars, aes(x = hp, y = mpg, color = factor(cyl)) +
geom_point() +
geom_smooth()