Hi there I am trying to create an animated heatmap of CO2 emissions over time on a world map with a result something similar to this (https://ourworldindata.org/grapher/annual-co2-emissions-per-country?tab=map&time=1846). After a few attempts my approach has been to use the rworldmap and gganimate packages to firstly merge my CO2 emissions data (three columns 'Country' 'Year' 'CO2 Emissions') with the map data, secondly create an initial plot of the first year of data (1850) and then animate it.
I am struggling to make the initial plot as i suspect my combined map and CO2 data, merged by the line of code:
CO2_data_with_map \<- joinCountryData2Map(CO2_data_filtered, joinCode = "NAME", nameJoinColumn = "Country")
has not merged effectively.
This is the current code which is not yielding an initial plot:
num_colors <- 5 CO2_color_scale <- brewer.pal(num_colors, "Spectral")
initial_year <- min(CO2_data_with_map$Year)
`p <- ggplot() +
geom_polygon(data = CO2_data_with_map, aes(x = long, y = lat, group = group, fill = `CO2 emissions`)) +
scale_fill_gradientn(colours = CO2_color_scale, name = "CO2 Emissions") +
labs(title = paste0("Year: ", prettyNum(initial_year, big.mark = ","))) +
theme_map() +
theme(plot.title = element_text(size = 20, face = "bold", margin = margin(0,0,10,0)))`
when I print(p) I receive error code:
Error in geom_polygon(): ! Problem while computing aesthetics. ℹ Error occurred in the 1st layer. Caused by error in FUN(): ! object 'CO2 emissions' not found Run rlang::last_trace() to see where the error occurred.
but when I run names(CO2_data_with_map) CO2 emissions is one of many columns.
Can anyone help by either suggesting a fix or different approach, it would be greatly appreciated, please ask if you need any more code