wb_income_groups %>% head()
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")
This is good enough if we’re just playing around with the data for our own understanding. But you wouldn’t want to share this with others. It’s ugly. We don’t know what we’re looking at. We don’t know the units. We don’t know where the data comes from.
But cleaning it up is pretty easy.
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")
Now we’ve cleaned it up into a format that we can share. While this may look like a lot of code, remember that good programmers work hard to be lazy.
Here are some great resources for making great ggplot2 charts