Interactive and static data plots of source type CO2 emissions from 1920 to 2000.
pivot_longer
to show columns as source types of CO2 within the world.group_by
source so there will be an indication for each CO2 emission source type.mutate
to change Year so it will be displayed as end of year instead of the beginning of the year.e_charts
to create an e_charts
object wtih Year on the x-axis.e_river
to create “rivers” that contain the amount of CO2 emissions for each source type.e_tooltip
to add a tooltip that will display based on the axis valuese_title
to add a title, subtitle, and link to subtitlee_theme
to change the theme tosource_type_co2 %>%
pivot_longer(cols = 2:7, names_to ="source_type", values_to = "amount") %>%
group_by(source_type) %>%
mutate(Year = paste(Year, "12", "31", sep = "-")) %>%
e_charts(x = Year) %>%
e_river(serie = amount, legend=FALSE) %>%
e_tooltip(trigger = "axis") %>%
e_title(text = "Annual CO2 Emissions, by source type",
subtext = "(in billions of tonnes). Source: Our World in Data",
sublink = "https://ourworldindata.org/grapher/co2-by-source",
left = "center") %>%
e_theme("roma")
ggplot
to create a new ggplot object. Use aes to indicate that Year will be the x-axis; CO2 emissions will be the y-axis; Source type will be fill variable.geom_area
will display Source Typescale_fill_discrete_divergingx
is a function in the colorspace package, this will set the color package to roma
and selects a max of 12 colors to indicate the different source types.theme_classic
sets the themetheme(legend.position = "bottom")
puts the legend at the bottom of the plotlabs
sets the y-axis label, fill = NULL indicates that the fill variable will not have the labelled source type.