Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
Created April 7, 2013 19:02
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 chrishanretty/5331977 to your computer and use it in GitHub Desktop.
Save chrishanretty/5331977 to your computer and use it in GitHub Desktop.
Plot votes for Italian presidential election candidates
dat <- read.csv("votazioni.csv",header=T)
## Exclude schede bianche, schede null
dat <- subset(dat,!is.element(Candidato,c("Schede bianche","Schede nulle","Voti dispersi")))
## Iterate over years
for (my.year in unique(dat$Year)) {
my.subset <- subset(dat,Year == my.year)
## top eight candidates
max.per.candidate <- tapply(my.subset$Voti,my.subset$Candidato,max)
max.per.candidate <- sort(max.per.candidate,decreasing=TRUE)[1:8]
my.subset <- subset(my.subset,Candidato %in% names(max.per.candidate))
my.subset$Candidato <- factor(my.subset$Candidato)
my.palette <- rep(brewer.pal(4,"Set1"),times=2)
my.ltys <- rep(c(2,3),each=4)
my.pchs <- rep(c(19,0),each=4)
my.xlims <- range(my.subset$Scrutinio)
my.ylims <- range(my.subset$Voti)
## Open the svg device
outfile <- paste0(my.year,"_plot.svg")
devSVGTips(outfile, toolTipMode=1,
title=paste0(my.year," presidential election"),
width=8.5,height=5)
plot(my.xlims,my.ylims,type="n",
main = paste0(my.year, " election"),
xlab="Scrutinio",ylab="Votes",
#log="y",
axes=FALSE)
axis(1,at = 1:max(my.subset$Scrutinio),labels=as.character(1:max(my.subset$Scrutinio)))
axis(2)
for (i in 1:length(levels(my.subset$Candidato))) {
relevant <- which(my.subset$Candidato == levels(my.subset$Candidato)[i])
points(my.subset$Scrutinio[relevant],my.subset$Voti[relevant],
type = "b",
cex = 1.2,
pch = my.pchs[i],
lty = my.ltys[i],
lwd = 3,
col = my.palette[i])
## add tips
for (j in 1:length(relevant)) {
setSVGShapeToolTip(title = my.subset$Candidato[relevant[j]],desc1= paste0(my.subset$Voti[relevant[j]]," votes this round"))
points(my.subset$Scrutinio[relevant[j]],my.subset$Voti[relevant[j]],cex=1.2,pch=my.pchs[i],col=my.palette[i])
}
}
dev.off()
## now do a static plot
outfile <- paste0(my.year,"_plot.png")
png(outfile,width=625,height=420)
par(xpd=T, mar=par()$mar+c(0,0,0,12))
plot(my.xlims,my.ylims,type="n",
main = paste0(my.year, " election"),
#log="y",
xlab="Scrutinio",ylab="Votes",
axes = FALSE)
axis(1,at = 1:max(my.subset$Scrutinio),labels=as.character(1:max(my.subset$Scrutinio)))
axis(2)
for (i in 1:length(levels(my.subset$Candidato))) {
relevant <- which(my.subset$Candidato == levels(my.subset$Candidato)[i])
points(my.subset$Scrutinio[relevant],my.subset$Voti[relevant],
type = "b",
cex = 1.2,
pch = my.pchs[i],
lty = my.ltys[i],
lwd = 3,
col = my.palette[i])
}
legend(my.xlims[2]+(my.xlims[2]-my.xlims[1])*.1,my.ylims[2],legend = levels(my.subset$Candidat),lty = my.ltys,pch=my.pchs,col=my.palette,bty="n")
dev.off()
par(mar=c(5, 4, 4, 2) + 0.1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment