Is `ggplot(data, aes())` equivalent to `ggplot(data) + aes()`?

While putting the aes function inside a geom changes its behavior from local to global, putting it outside both geoms and ggplot() seems to have the same effect as putting it inside `ggplot(). This means that code that I usually see written as:

ggplot(gapminder, aes(x = year, y = lifeExp, color = continent, size = pop)) +
    geom_line(stat = 'summary', fun = mean)

could be written as

ggplot(gapminder) +
    aes(x = year,
        y = lifeExp,
        color = continent,
        size = pop) +
    geom_line(stat = 'summary', fun = mean)

Which I personally find visually tidier, easier to manipulate, and avoids the function within function situation which can lead to missed closing double parenthesis at the end.

Both approaches change the global aesthetics but I want to double check if they are truly equivalent so that they can be used interchangeably, or if there are some corner cases when using the latter approach?

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.