Я пытаюсь найти одношаговое решение.А пока вот простое двухэтапное решение -
pop_1952 <- filter(gapminder, year == 1952) %>%
group_by(continent) %>%
summarise(tot_pop_1952 = sum(pop, na.rm = T))
gapminder %>%
group_by(year, continent) %>%
summarize(tot_pop = sum(as.numeric(pop))) %>%
left_join(pop_1952, by = "continent") %>%
mutate(
CHG_SINCE_1952 = (tot_pop - tot_pop_1952) / tot_pop_1952
) %>%
ggplot(aes(x = year, y = CHG_SINCE_1952, color = continent)) +
geom_line()
Вот решение в одной цепочке, если это помогает (все же, технически, два шага, я думаю) -
gapminder %>%
mutate(
tot_pop_1952 = ave(as.numeric(pop)*(year == 1952), continent, FUN = sum)
) %>%
group_by(year, continent) %>%
summarize(
tot_pop = sum(as.numeric(pop)),
tot_pop_1952 = mean(tot_pop_1952),
CHG_SINCE_1952 = (tot_pop - tot_pop_1952) / tot_pop_1952
) %>%
ggplot(aes(x = year, y = CHG_SINCE_1952, color = continent)) +
geom_line()