Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Contributions per Candidate
# read csv file
ds <- read.csv('2012-Presidential-Campaign-Finance-Contributors.tsv', sep='\t')
# aggregate by candidate
res <- aggregate(ds$contb_receipt_amt, by=list(ds$cand_nm), FUN=sum, na.rm=TRUE)
X = data.frame(name=res$Group.1, amount=res$x)
# make data columns callable without typing X$ everytime
attach(X)
# sort dataset by amount
X <- X[order(amount),]
# assign party and colors
X$party[name == "Obama, Barack"] <- "Democrats"
X$color[name == "Obama, Barack"] <- "#2255EE"
X$party[name != "Obama, Barack"] <- "Republicans"
X$color[name != "Obama, Barack"] <- "#EE2244"
X$party <- factor(X$party)
# create a nicer labels like "Obama, Barack ($123.4 mio)"
X$labels <- sprintf("%s ($%.1f mio)", name, amount/1000000)
detach(X)
attach(X)
# output dot chart
dotchart(amount/1000000, labels=labels, color=color, groups=party,
main="Contributions per Candidate", xlab="Amounts in Mio $", pch=19, cex=.8)

makoto commented May 7, 2012

Hi. I came across this gist through http://datajournalismhandbook.org/1.0/en/understanding_data_7.html . Do you mind if you can let me know where I can download the data files (fec-contr.csv and 2012-Presidential-Campaign-Finance-Contributors.tsv) ? Thanks . Makoto

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