Animated Surface Maps
Last updated
Last updated
The Earth's surface temperature has changed significantly since the 20th century. One way to visualize these changes is through a map of all the countries and their respective temperature differences.
In this section, we'll be making this plot:
The data is accessible here (GlobalLandTemperaturesByCountry.csv).
As usual, we'll load in our libraries first.
We'll now load in a map of the world. Perviously, we did this with the maps
package, but I'm now using rnaturalearth
. There's no big difference between the two for this type of plot, so we'll explore the other option for now.
We'll also read in our data.
When we eventually plot, we need to join together the two datasets. Unfortunately, some of the names of countries are not equal between the two datasets, so we'll need to manually adjust those now.
Now that we've adjusted, we can do a left join on the world data frame, which contains the geometry.
Since we're going to be iterating over dates, we need to make sure that R interprets the date column as Date
objects.
With our data in the proper format, we can now filter it down.
In this code segment, we're just making a new column for the year of every data point, and filtering to the years 1985 and greater. Then, to make the data easier to handle, we're only selecting the three most important columns, which we'll soon use in our plot.
Since we're comparing each year's surface temperature with the 20th century average (for each country), we need to obtain that data.
We're doing a very similar manipulation of the data here, making a new column for the year, then filtering to only include 20th century values. After that, we group by every country, then find the average surface temperature in the 20th century for that country.
With this in place, we can then join average_20th
back with the world_plot
data, to have all the data in one place.
The data is still in need of some cleaning though. We need to have just one value for each year, not 12 (for all the months).
We're just filtering for actual values, then grouping by country and year, and formatting the data accordingly. We're taking the first value for all the columns where the data doesn't change, and the mean for the average temperature (which changes per month). Then, we find the difference between the average temperature of that year and the average 20th century value.
After that, we'll have a data frame with each country, the year, the surface temperature for that year, and the 20th century average.
We're now ready to plot.
With this basic structure, we're supplying our world_plot
data frame to the ggplot
object, then accessing the difference column to fill each country. Finally, we're transitioning through time for the years 1985 through 2013, so we give the range (as an integer) as well as the year variable to the transition_time
method.
After a few minutes, you'll get this plot:
We have all the data in the right place, but we can make this look better.
Note that we're using frame_time in the subtitle, but since we indicated in transition_time that we're iterating over integers, we don't need to do any casting.
We then supply this animation to the animate function, which yeilds our final plot.
We're finished! We've now made a very clear and insightful plot of animated surfaces.