Practical Worksheet


Package and Data Loading


As mentioned within the session setup, load the following packages using the library() function. Additionally, as we will be using a data set with large numbers, set scipen to 999 using the option function.

  library(tidyverse)
  library(RColorBrewer)

  options(scipen = 999)

Furthermore, for the purpose of this session, we will be using data from the World Bank Open Data. In particular we will be using a collection of variables from 1999, these variables were selected to provide us plenty of room to explore!

It is included in your downloaded zip file from the accompanying Github Repo and can be loaded using the following code:

WBD_1999 <- read_csv("data/WBD_1999.csv")

It includes the following variables:

  • Country Name (Country Name)
  • Country Code (Country Code)
  • Continent (Continent)
  • Year (Year)
  • Population (Pop)
  • Female Population (Pop.fe)
  • Male Population (Pop.ma)
  • Birth Rate, crude per 1000 people (birthrate)
  • Death Rate, crude per 1000 people (deathrate)
  • Life Expectancy at Birth in years (lifeexp)
  • Female Life Expectancy at Birth in years (lifeexp.fe)
  • Male Life Expectancy at Birth in years (lifeexp.ma)
  • Educational Spending, percentage of GDP (ed.spend)
  • Compulsory Education Duration in Years (ed.years)
  • Labour Force Total (labour)
  • Literature Rate in adults, percentage % (lit.rate.per)
  • CO2 Emissions, kt (co2)
  • Gross Domestic product, $ (gdp)
  • Unemployment, percentage of total labour force (unemp)
  • Female Unemployment, percentage of total labour force (unemp.fe)
  • Male Unemployment, percentage of total labour force (unemp.ma)
  • Health Expenditure per capita, $ (health.exp)
  • Hospital Beds per 1000 people (medbeds)
  • Number of Surgical Procedures per 1000 people (surg.pro)
  • Number of Nurses & Midwives per 1000 people (nurse.midwi)

Section 1: ggplot2 vs plot


Exercise 1: Plotting birthrate against deathrate using both the plot() and ggplot() function, discuss which has more potential in displaying data clearly.


Exercise 2: Expand the plot to group these points by Continent, which provides us with more information and is easier to achieve? Remember, you’ll need to recode WBD_1999$Continent as a factor using the function:

WBD_1999$Continent <- as.factor(WBD_1999$Continent)



Section 2: Scatter Plots in ggplot


Exercise 3: Change the size parameter to ed.years to see if there is a trend between amount of years in Education and the Birth and Death Rate, set the alpha parameter to 0.5 to clearly see the relationships.


Exercise 4: Change the Labels on the X and Y axis’ and provide a suitable title for the graph



Section 3: Bar Charts and Histograms


Exercise 5: Using the parameter stat = "identity" within the geom_bar() function, create a bar chart of Continent plotted against the mean birthrate or deathrate


Exercise 6: Using the function geom_histogram() create a histogram of the birthrate and deathrate



Section 4: Adding density plots to Histograms


Exercise 7: Using the plot created in exercise 6, add the y-variable ..density.. and binwidth = 1 to geom_histogram() in addition to adding geom_density() to add density lines to the Histogram


Exercise 8: Add the parameter, adjust = 2 in the density plot, to smooth this link and make it more easily interpretable



Section 5: Extra Useful Tips and Functions


Exercise 9: Add a legends() and breaks() to a plot


Exercise 10: Use the ggsave() function to save your last plot