Skip to content

Instantly share code, notes, and snippets.

@JoesDataDiner
Created October 13, 2013 16:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoesDataDiner/6964393 to your computer and use it in GitHub Desktop.
Save JoesDataDiner/6964393 to your computer and use it in GitHub Desktop.
UK House Prices vs. Household Incomes
rm(list=ls())
library(plyr)
library(ggplot2)
library(RColorBrewer)
price_data_dir = "C:\\***\\"
price_data_filepaths = paste(price_data_dir,list.files(price_data_dir),sep="")
price_data_list = lapply(price_data_filepaths,FUN<-function(x) read.csv(x,header=FALSE))
price_data = do.call(rbind,price_data_list) #this gives about 1.5mil transactions during 2010-2011
price_percentiles = quantile(price_data[,2],probs = seq(0.01,0.99,by=0.01)) #we know column 2 contains the prices
income_data_filepath = "C:\\***\\Income.csv"
income_data = read.csv(income_data_filepath)
income_percentiles = income_data[,2] * 52 # data is weekly income percentiles
ratio_data = data.frame(percentile = 1:99,
price = price_percentiles,
income = income_percentiles,
ratio = price_percentiles / income_percentiles)
#copying the gaurdians different shading fo 10% points and the median
ratio_data$percentile_rank = rep(3,nrow(ratio_data))
ratio_data$percentile_rank[!is.na(match(ratio_data$percentile,1:9*10))] = 2
ratio_data$percentile_rank[!is.na(match(ratio_data$percentile,50))] = 1
ratio_data$percentile_rank = factor(ratio_data$percentile_rank,c(1:3))
bespoke_colour_set = c("black",brewer.pal(9,"Set1")[c(5,1)])
#plot the results
png(filename = "C:\\***\\income.png",
width = 700,
heigh = 350)
income_graph<-ggplot(ratio_data,aes(x = percentile,y = income / 1000,fill = percentile_rank)) +
geom_bar(stat='identity',width=0.5) +
ylab("Household Income / k£") +
guides(fill=FALSE) +
scale_fill_manual(values = bespoke_colour_set)+
scale_x_continuous(breaks = 1:9*10)+
theme_bw()+
theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank())
print(income_graph)
dev.off()
png(filename = "C:\\***\\price.png",
width = 700,
heigh = 350)
price_graph<-ggplot(ratio_data,aes(x = percentile,y = price / 1000,fill = percentile_rank)) +
geom_bar(stat='identity',width=0.5) +
ylab("House Price / k£") +
guides(fill=FALSE) +
scale_x_continuous(breaks = 1:9*10)+
scale_fill_manual(values = bespoke_colour_set)+
theme_bw()+
theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank())
print(price_graph)
dev.off()
png(filename = "C:\\***\\ratio.png",
width = 750,
heigh = 350)
ratio_graph<-ggplot(ratio_data,aes(x = percentile,y = ratio,colour = percentile_rank)) +
geom_point() +
ylab("Household Income / House Price") +
guides(colour=FALSE) +
scale_x_continuous(breaks = 1:9*10)+
scale_colour_manual(values = bespoke_colour_set)+
theme_bw()+
theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank())+
ylim(5,11)
print(ratio_graph)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment