I am using the WHO dataset in R and currently having trouble collapsing the age section into five groups as seen in this picture.
I have pasted a copy of my code below, anything helps.
names(who) <- str_replace(names(who), "newrel", "new_rel")
who2 <- who %>%
gather("codes", "case", 5:60) %>%
select(-iso2, -iso3) %>%
separate(codes, c("new", "type", "sexage"), sep = "_") %>%
select(-new) %>%
separate(sexage, into = c("sex", "age"), sep = 1,convert=TRUE)
who2 %>%
filter(!is.na(case)) %>%
group_by(year, sex, age) %>%
mutate(cases = sum(case)) %>%
mutate(age = factor(age, levels=c("14","1524", "2534","3544","4554","5564","65"),ordered=TRUE)) %>% mutate(age = fct_recode(age, "0-14" = "14", "15-24" = "1524", "25-34" = "2534", "35-44" = "3544", "45-54" = "4554", "55-64" = "5564", "65+" = "65")) %>%
ggplot() +
geom_line(mapping = aes(x = year, y = cases, color = age)) +
facet_wrap(~sex)