Skip to content

Instantly share code, notes, and snippets.

@mikebirdgeneau
Last active October 10, 2015 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikebirdgeneau/021bc7d89ab314ec6e85 to your computer and use it in GitHub Desktop.
Save mikebirdgeneau/021bc7d89ab314ec6e85 to your computer and use it in GitHub Desktop.
Plot GDP per Capita for G7 countries, normalized to when the Harper government took power
library(ggplot2)
library(data.table)
library(lubridate)
# Source: https://stats.oecd.org/index.aspx?queryid=350
dt.gdp<-data.table(read.csv("~/Downloads/QNA_10102015180610407.csv"))
dt.gdp[,Year:=substr(TIME,1,4),]
dt.gdp[,Qtr:=substr(TIME,7,7),]
dt.gdp[,Month:=as.numeric(Qtr)*3-1,]
dt.gdp[,Date:=as.Date(paste0(Year,"-",formatC(Month,width=2,flag="0"),"-15")),]
plot.data<-dt.gdp[Country %in% c("Canada","France","Germany","Italy","Japan","United Kingdom","United States") & MEASURE=="HVPVOBARSA" & Date>=as.Date("2000-01-01"),]
# Normalize to Q1-2006 (When the current government assumed power)
plot.data[Date==as.Date("2006-02-15"),Reference.Value:=Value,]
plot.data[,Reference.Value:=mean(Reference.Value,na.rm=TRUE),by=c("Country")]
plot.data[,Normalized.Value:=Value/Reference.Value,]
plot.data$Reference.Value<-NULL
p1<-ggplot(plot.data,aes(x=Date,y=Normalized.Value,colour=Country))+geom_line()+geom_line(data=plot.data[Country=="Canada"],lwd=0.9)+ylab("Normalized GDP Per Capita (USD)")+xlab("Date\nData Source: https://stats.oecd.org/index.aspx?queryid=350")+theme_bw()+ggtitle("G7 Countries: Relative Change in GDP Per Capita, Q1-2006 = 1.00")
print(p1)
# Normalize to Q1-2011 (When the current government assumed power)
plot.data<-dt.gdp[Country %in% c("Canada","France","Germany","Italy","Japan","United Kingdom","United States") & MEASURE=="HVPVOBARSA" & Date>=as.Date("2000-01-01"),]
plot.data[Date==as.Date("2011-02-15"),Reference.Value:=Value,]
plot.data[,Reference.Value:=mean(Reference.Value,na.rm=TRUE),by=c("Country")]
plot.data[,Normalized.Value:=Value/Reference.Value,]
plot.data$Reference.Value<-NULL
p2<-ggplot(plot.data,aes(x=Date,y=Normalized.Value,colour=Country))+geom_line()+geom_line(data=plot.data[Country=="Canada"],lwd=0.9)+ylab("Normalized GDP Per Capita (USD)\n")+xlab("Date\nData Source: https://stats.oecd.org/index.aspx?queryid=350")+theme_bw()+ggtitle("G7 Countries: Relative Change in GDP Per Capita, Q1-2011 = 1.00")
print(p2)
library(gridExtra)
grid.arrange(p1,p2,nrow=2)
png("~/Downloads/GDP_G7.png",width=1600,height=1000,res=150)
grid.arrange(p1,p2,nrow=2)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment