The data have been scraped from the BBC website by Kieran Healey. The following line clones his github repository.
git clone https://github.com/kjhealy/uk-elections.git
rm(list=ls())
library(rgdal)
map <- readOGR("maps/topo_wpc.json", "wpc")
## OGR data source with driver: GeoJSON
## Source: "maps/topo_wpc.json", layer: "wpc"
## with 632 features and 2 fields
## Feature type: wkbPolygon with 2 dimensions
constituency_names.df<-read.csv("data/constituency_names.csv")
ind <- match(map@data$id, constituency_names.df$id)
map@data$name <- constituency_names.df$Constituency[ind]
map@proj4string<-CRS("+init=epsg:4326")
writeOGR(map,"data","constituencies_UK","ESRI Shapefile",over=TRUE)
#map<-readOGR("data","constituencies")
dd<-map@data
dd$order<-as.numeric(row.names(dd)) # Need this later to make sure the polygons and the data frame line up.
plot(map)
library(reshape)
d<-read.csv("data/uk-election-results-2015.csv")
#write.csv(d,"data/election-results.csv",row.names=F)
d$Party.all[d$Candidate == "John Bercow"] <- "Conservative"
d$Party[d$Candidate == "John Bercow"] <- "Conservative"
d<-d[d$Party.all%in%c("Conservative","Labour","Liberal Democrat","Green Party","UKIP","Plaid Cymru","Scottish National Party"),]
d<-d[d$Region!="Northern Ireland",]
d<-droplevels(d)
d<-d[order(d$Constituency),]
d$rank<-unlist(tapply(-d$Votes,d$Constituency,order))
win<-subset(d, rank==1)
win<-win[,2:3]
names(win)<-c("Constituency","Winner")
second<-subset(d, rank==2)
second<-second[,2:3]
names(second)<-c("Constituency","Second")
ds<-melt(d,id=c(2,3,4),m=6)
d1<-cast(ds,Constituency~Party,sum)
d1<-merge(d1,win,by.x="Constituency",by.y="Constituency")
d1<-merge(d1,second,by.x="Constituency",by.y="Constituency")
d1<-merge(dd,d1,by.x="name",by.y="Constituency",sort=TRUE)
d1<-d1[order(d1$order),]
row.names(d1)<-d1$order
names(d1)
## [1] "name" "id"
## [3] "order" "Conservative"
## [5] "Green Party" "Labour"
## [7] "Liberal Democrat" "Plaid Cymru"
## [9] "Scottish National Party" "UKIP"
## [11] "Winner" "Second"
names(d1)<-c("name","id","order","Con","Green","Lab","LibDem","Plaid","SNP","UKIP","Win","Second")
map@data<-d1
writeOGR(map,"data","percent-votes",driver="ESRI Shapefile",over=TRUE)
Now upload zipped shapefile to CartoDB
File available here.