Created
February 7, 2012 23:22
-
-
Save dfkoz/1762931 to your computer and use it in GitHub Desktop.
R source code for three simple graphs -- summary bar chart, histogram, and duration curve -- based on Super PAC contribution data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Set the default working directory & load the data | |
| setwd("C:\\Users\\owner\\Dropbox\\Other\\Super PAC Data\\") | |
| pac <- read.csv("Super_PAC.csv") | |
| # First, let's segment the data by political leaning | |
| liberal_pac <- subset(pac, pac$C.L == "Liberal") | |
| conserv_pac <- subset(pac, pac$C.L == "Conservative") | |
| nonpart_pac <- subset(pac, pac$C.L == "Nonpartisan") | |
| ################################################ | |
| ############ SIMPLE BAR CHART ################## | |
| # Get some of the chart basics configured | |
| totals <- c(sum(liberal_pac$Amount), sum(conserv_pac$Amount), sum(nonpart_pac$Amount)) | |
| colors <- c("blue", "red", rgb(0.2, 0.2, 0.2)) | |
| bar_labels <- c("$0", "$30M", "$60M") | |
| bar_labels_at <- c(0, 30000000, 60000000) | |
| # Now start plotting. | |
| mp <- barplot(totals, | |
| col=colors, | |
| ylim=c(0, 70000000), | |
| axes=F, | |
| names.arg=c("Liberal", "Conservative", "Non-partisan"), | |
| font.axis=2, | |
| main="Total Donations by Viewpoint", | |
| cex.main=4.5, | |
| cex.names=2.0 | |
| ) | |
| box() | |
| axis(2, labels=bar_labels, at=bar_labels_at, font.axis=2, cex.axis=2.0) | |
| ################################################ | |
| ############ HISTOGRAM ######################### | |
| # We're going to graph log(donation), so let's | |
| # set up some meaningful labels | |
| for (i in 2:6) { | |
| # Do two buckets at a time: the first with i zeros, and | |
| # the second halfway to i + 1 zeros | |
| pac_buckets <- append(pac_buckets, i) | |
| pac_labels <- append(pac_labels, sprintf("$%.0f", 10 ^ i)) | |
| pac_buckets <- append(pac_buckets, i + 1 / 2) | |
| pac_labels <- append(pac_labels, sprintf("$%.0f", 10 ^ (i + 1) / 2)) | |
| } | |
| # Forgot one... | |
| pac_buckets <- append(pac_buckets, 7) | |
| pac_labels <- append(pac_labels, "$10000000") | |
| # Now, set up a series of overlapping histograms | |
| # based on the log10 of each donation amount, red/blue for | |
| # conservative/liberal, and manually-drawn axes. | |
| hist(log(conserv_pac$Amount, 10), | |
| col=rgb(1, 0, 0, 1.0), | |
| xlim=c(2, 7), | |
| axes=FALSE, | |
| main="Histogram of Super PAC Donations by Political Viewpoint", | |
| xlab="Donation Amount", | |
| ylab="Count", | |
| font.lab=2, | |
| cex.main=2.5, | |
| cex.lab=1.5 | |
| ) | |
| hist(log(liberal_pac$Amount, 10), | |
| col=rgb(0, 0, 1, 1.0), | |
| axes=FALSE, | |
| xlim=c(2, 7), | |
| ylim=c(0, 250), | |
| add=T, | |
| xlab=F, | |
| ylab=F, | |
| main=F | |
| ) | |
| axis(1, | |
| labels=pac_labels, | |
| at=pac_buckets, | |
| font.axis=2 | |
| ) | |
| axis(2, font.axis=2) | |
| ############################################## | |
| ################################################ | |
| ############ DURATION CURVE #################### | |
| liberal_pac <- liberal_pac[with(liberal_pac, order(Amount, decreasing=FALSE)),] | |
| conserv_pac <- conserv_pac[with(conserv_pac, order(Amount, decreasing=FALSE)),] | |
| nonpart_pac <- nonpart_pac[with(nonpart_pac, order(Amount, decreasing=FALSE)),] | |
| plot(conserv_pac$Amount, | |
| type="o", | |
| col="red", | |
| axes=F, | |
| main="Donations by Political View", | |
| cex.main=2.0, | |
| xlab="# of Donations", | |
| ylab="Amount", | |
| font.lab=1.5, | |
| cex.lab=1.5 | |
| ) | |
| lines(liberal_pac$Amount, | |
| type="o", | |
| col="blue" | |
| ) | |
| lines(nonpart_pac$Amount, | |
| type="o", | |
| col=rgb(0.2, 0.2, 0.2) | |
| ) | |
| box() | |
| axis(2, labels=c("0", "$5,000,000"), at=c(0, 5000000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment