Wealth and Life Expectancy

Visualize changes over time and space
More advanced ggplot plus spatial data visualization
Tasks
• Recreate layered graphics with ggplot including raw and transformed data
• Save graphical output as a .png file
• Save your script
Background
Hans Rosling is one of the most popular data scientists on the web. His original TED talk was very popular when it came out. We are going to create some graphics using his formatted data in our case study. Note that we need to remove Kuwait from the data (discussion on this at https://github.com/jennybc/gapminder/ issues/9).
Instructions
In this exercise you will recreate the three graphics shown below (Page 3-4) using gapminder dataset from gapminder package and world dataset from spData package. Get your graphs to match as closely as you can. Specific instructions/steps are listed in the ‘Detailed Steps’ section.
Detailed Steps
1. Load the necessary packages.
• Use filter() to remove “Kuwait” from the gapminder dataset
2. Plot #1
• Use ggplot() and the theme_bw() to make the first plot using the filtered dataset (without Kuwait)
• Specify the appropriate aesthetic mapping (aes()) to color by continent and adjust the size of the point with size=pop/100000. Remember that if you adjust the data like this you will also need to update the legend title.
• Use Dark2 color palette.
• Use scale_y_continuous(trans = “sqrt”) to get the correct scale on the y-axis.
• Use a facet function to divide the plot into separate panels.
• Specify more informative x and y axis labels.
3. Prepare the data for the second plot
• Load world data from spData package.
• Change the name_long column name to country to match the gapminder data.
• Calculate the increase in life expectancy between 1957 and 2007 for each country.
• Join the calculated data to world data.
4. Plot #2
• Use ggplot() and the theme_bw() to make the second plot using the joined world data.
• Use Reds color palette. Change legend title.
5. Prepare data for the third plot
• Use st_centroid to get the centroid points from world data.
6. Plot #3
• Use 2014 life expectancy and GDP per capita from world data in this plot.
• Use YlGnBu color palette for the life expectancy.
• Plot gdpPercap at the centroid points of each country by size.
• Change legend titles.
7. Use ggsave() or png() and save each plot as a .png
8. Check your script to confirm that it runs from start to finish without errors and saves the graphics.
9. Save your script
What you submit
You need to submit:
1. your R script file (CS04_yourname.R),
2. Three output graphs files – three png files (e.g. Fig1_yourname.png),
3. Briefly explain the information of each graph in a word or pdf file.
Visualize changes over time and space
• Plot 1 Wealth and life expectancy through time
• Plot 2 Map of life expectancy in each country in 2007
• Plot 3 Map of life expectancy and GDP per capita in each country in 2014

Figure 1: Wealth and life expectancy through time

Figure 2: Map of life expectancy in each country in 2007

Figure 3: Map of life expectancy and GDP per capita in each country in 2014