Question
Is it possible to map an aesthetic to the mean value for each group in a dataset?
Example
Map the line width for each continent in gapminder, but avoid pre-processing the data to create the group means for this mapping. The following plot is the desired output:
library(ggplot2)
library(gapminder)
ggplot(data = gapminder %>% group_by(continent) %>% mutate(mean_pop = mean(pop)),
mapping = aes(x = year,
y = lifeExp,
color = continent,
linetype = continent,
size = mean_pop)) +
geom_line(stat = "summary")
But I want data to just take gapminder as-is:
ggplot(data = gapminder,
mapping = aes(x = year,
y = lifeExp,
color = continent,
linetype = continent,
...)) +
geom_line(stat = "summary")
