wb_income_groups %>% head()
Here’s a basic chart
emissions_dataset %>%
# filter for latest data
filter(year == 2019) %>%
# get the top 5 by cumulative Co2 per capita
slice_max(order_by = cumulative_co2_per_capita, n = 5) %>%
# fct_reorder will sort x by cumulative Co2 per capita
ggplot(aes(x = fct_reorder(.f = country_name,.x = cumulative_co2_per_capita,
.desc = TRUE),
# fill will change the color by whether a country is EM or DM
y = cumulative_co2_per_capita, fill = em_dm)) +
# just get used to the `stat = identity` thing for bar charts, default is `count`
geom_bar(stat = "identity")
Here’s that chart cleaned up for presentation
emissions_dataset %>%
# filter for latest data
filter(year == 2019) %>%
# get the top 5 by cumulative Co2 per capita
slice_max(order_by = cumulative_co2_per_capita, n = 5) %>%
# fct_reorder will sort x by cumulative Co2 per capita
ggplot(aes(x = fct_reorder(.f = country_name,.x = cumulative_co2_per_capita,
.desc = TRUE),
# fill will change the color by whether a country is EM or DM
y = cumulative_co2_per_capita, fill = em_dm)) +
# just get used to the `stat = identity` thing for bar charts, default is `count`
geom_bar(stat = "identity") +
geom_text(aes(label = format(cumulative_co2_per_capita, digits = 0, big.mark = ",")), vjust = -0.2) +
# expand the y limits so the text doesn't get cut off
scale_y_continuous(limits = c(0, 1500), breaks = scales::pretty_breaks(n = 10)) +
# you can use color names or hex codes
scale_fill_manual(values = c("Developed Markets" = "grey", "Emerging Markets" = "#008080")) +
# a minimal theme with mostly white space
theme_minimal() +
labs(title = "Wow, this is profound",
subtitle = "and some more context",
x = "", # it's obvious this is country
y = "Tons Co2 per capita",
fill = "", # this gets rid of the `em_dm` tag on top of the legend,
caption = "Data from x, Analysis by us"
) +
# moves the legend from the side (default) to the bottom.
theme(legend.position = "bottom")