if(!require(pacman)) install.packages("pacman")
pacman::p_load(tidyverse)
ghana <-
filter(tidyr::who, country == "Ghana") %>%
ggplot(aes(x = year, y = new_sp_m014)) +
geom_line()
nigeria <-
filter(tidyr::who, country == "Nigeria") %>%
ggplot(aes(x = year, y = new_sp_m014)) +
geom_line()
kenya <-
filter(tidyr::who, country == "Kenya") %>%
ggplot(aes(x = year, y = new_sp_m014)) +
geom_line()Lots of code repetition above. Write a function instead:
plot_who_tb <- function(df, country_name) {
filter(df, country == {{country_name}}) %>%
ggplot() +
geom_line(aes(x = year, y = new_sp_m014))
}
# Now apply it:
ghana <- plot_who_tb(tidyr::who, "Ghana")
nigeria <- plot_who_tb(tidyr::who, "Nigeria")
kenya <- plot_who_tb(tidyr::who, "Kenya")