Skip to content

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
# 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)
# output dot chart
dotchart(amount/1000000, labels=labels, color=color, groups=party,
main="Contributions per Candidate", xlab="Amounts in Mio $", pch=19, cex=.8)

This comment has been minimized.

Copy link

@makoto makoto commented May 7, 2012

Hi. I came across this gist through . 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