We will use data from the gapminder
package in these examples
Note that the range of the axes is taken from the data
p +
1 geom_smooth()
Note that points are no longer displayed: adding elements to a plot creates a new plot, leaving the input untouched.
Elements can be stacked on top of each other.
What happens if we change the order?
p + geom_point() +
geom_smooth() +
1 scale_x_log10()
x
scale logarithmic
Scale transformations are applied before fitting the model line
p + geom_point() +
geom_smooth() +
scale_x_log10(
1 labels = scales::dollar
)
x
ticks as dollars
All layers are functions, and as such they accept (optional) arguments to customize their behavior
p <- ggplot(data = gapminder,
mapping = aes(
x = gdpPercap,
y = lifeExp,
1 color=continent
))
p + geom_point() +
geom_smooth(method = "gam") +
scale_x_log10(labels = scales::dollar)
This is quite a mess! How can we address it?
Here we are fixing an aesthetic attribute to a specific value, for a single layer
Notice that, differently from before, we are not mapping a data variable to an aesthetic variable.
"gray"
is added to the input data frame"gray"
stringcontinent
here
continent
, hence we get a single global smoothing line