Graphing Population Projections

Let's start by installing and then loading a nice graphing package called ggplot2.

install.packages("ggplot2")
## Error: trying to use CRAN without setting a mirror
library(ggplot2)

Depending on how you opened the files, you might need to set your working directory. Let's check.

getwd()
## [1] "/Users/ericpgreen/Desktop"

If the result does not point to your desktop, you'll need to change the working directory.

setwd("/Users/ericpgreen/Desktop")

You can check to make sure it's correct by running getwd() again. Assuming it is, we can import some data. The population data we are going to use come from the UN (2012 revision).

dat <- read.csv("popdata.csv")

You can open the data frame or just run the following command to get a sense of the structure.

head(dat)
##   year   value variant   who
## 1 2010 6916183    high world
## 2 2011 7005845    high world
## 3 2012 7099195    high world
## 4 2013 7195452    high world
## 5 2014 7293436    high world
## 6 2015 7392233    high world

The data are organized in a long format, so every year appears six times: variant (low, medium, high) times who (world, ssa). We can limit the dataset to just the world projection as follows:

dat.w <- subset(dat, dat$who == "world")

Let's plot the world data.

ggplot(dat.w, aes(x = year, y = value, group = variant, colour = variant)) + 
    geom_line()

plot of chunk plot

It's OK, but we can clean up our labels a bit.

ggplot(dat.w, aes(x = year, y = value, group = variant, colour = variant)) + 
    geom_line() + ylab("Total Population, Both Sexes Combined") + xlab("Year")

plot of chunk plot2

We should divide by 10 million to make the y-axis easier to read.

ggplot(dat.w, aes(x = year, y = value/1e+07, group = variant, colour = variant)) + 
    geom_line() + ylab("Total Population, Both Sexes Combined (in billions)") + 
    xlab("Year")

plot of chunk y

Maybe we prefer a minimalist style…

ggplot(dat.w, aes(x = year, y = value/1e+07, group = variant, colour = variant)) + 
    geom_line() + ylab("Total Population, Both Sexes Combined (in billions)") + 
    xlab("Year") + theme_bw()

plot of chunk minimalist