Skip to content

Instantly share code, notes, and snippets.

@mikebirdgeneau
Last active October 10, 2015 20:35
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/1a87e232d77cb5d16a22 to your computer and use it in GitHub Desktop.
Save mikebirdgeneau/1a87e232d77cb5d16a22 to your computer and use it in GitHub Desktop.
Federal Spending on Science & Technology
library(ggplot2)
library(data.table)
library(lubridate)
library(stringr)
options(stringsAsFactors = FALSE)
# Data Source: Statistics Canada (Table 358-163)
#(http://www5.statcan.gc.ca/cansim/a26?lang=eng&retrLang=eng&id=3580163&&pattern=&stByVal=1&p1=1&p2=31&tabMode=dataTable&csid=)
dt.science <- data.table(read.csv("~/Downloads/03580163-eng.csv"))
dt.cpi <- data.table(read.csv("~/Downloads/03260021-eng.csv"))
dt.cpi<-dt.cpi[as.numeric(Ref_Date)>=2000 & GEO=="Canada" & COMM=="All-items",]
dt.cpi[,Year:=as.numeric(Ref_Date),]
dt.cpi[,CPI:=Value/100,]
dt.science[,Category:=str_replace_all(DEPART,"\\ \\(x 1,000,000\\)",""),]
dt.science[,Year:=as.numeric(substr(Ref_Date,1,4)),]
dt.science[,Value:=as.numeric(Value),]
dt.science <- merge(dt.science,subset(dt.cpi,select=c(Year,CPI)),by=c("Year"),all.x=TRUE)
dt.science[,Value.CPI.Adj:=Value/CPI,]
setkeyv(dt.science,c("Year","Category"))
plot.data<-dt.science[Year<2015 & EXPENDITURES == "Total expenditures" & SCIENCE=="Total science and technology" & Category != "Total departments and agencies",list(Value=sum(Value,na.rm=TRUE),Value.CPI.Adj=sum(Value.CPI.Adj,na.rm=TRUE)),by=c("Year","Category")]
plot.data[,party:="Conservative",]
plot.data[Year<2006.167,party:="Liberal"]
p1<-ggplot(plot.data,aes(x=Year,y=Value.CPI.Adj/1000,fill=Category))+geom_area(stat="identity")+ylab("Expenditures (CAD, billions), Adjusted for Inflation")+ggtitle("Federal Expenditures on Science and Technology")+xlab("Year\nData Source: Statistics Canada (Table 358-0163 & 326-0021)")
p1
p2<-ggplot(plot.data,aes(x=Year,y=Value.CPI.Adj/1000,colour=party,group=NA))+geom_line()+facet_wrap(~Category)+geom_vline(xintercept=2006.167,lty=2)+scale_colour_manual(name="Government",values=c("Liberal"="red","Conservative"="blue"))+ggtitle("Canadian Federal expenditures on science and technology")+theme(legend.position='bottom',strip.text.x = element_text(size = 8))+xlab("Data Source: Statistics Canada (Table 358-0163 & 326-0021)")+ylab("Expenditures (CAD, billions), Adjusted for Inflation")
p2
png("~/Downloads/expenditures_science_by_dept.png",width=2880,height=1400,res=150)
print(p2)
dev.off()
png("~/Downloads/expenditures_science.png",width=1600,height=800,res=150,pointsize = 5.5,antialias = "default")
print(p1)
dev.off()
@mikebirdgeneau
Copy link
Author

Federal Spending on #science and technology decreasing since 2010?

elxn42 #cdnpoIi Source: https://gist.github.com/mikebirdgeneau/1a87e232d77cb5d16a22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment