File:Delegate share for the 2016 Democratic Party presidential primaries.svg

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Original file(SVG file, nominally 484 × 333 pixels, file size: 76 KB)

Captions

Captions

Add a one-line explanation of what this file represents

Summary

[edit]
Description
English: Delegate share for the 2016 Republican Party presidential primaries. Graph may not reflect the latest developments. Graph generated with an R script
Date
Source Own work
Author Abjiklam

Licensing

[edit]
I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.

Code

[edit]

The graph is generated by the following R script:

library(RCurl)
library(reshape)
library(htmltab)
library(ggplot2)
library(scales)

#get the table from the url
theurl <- getURL("https://en.wikipedia.org/wiki/Template:2016USDem", ssl.verifyPeer=FALSE)
table <- htmltab(theurl)

#keep only the useful columns and name them
df <- table[, c(1, 2, 9, 12)]
names(df) <- c("Date", "State", "Clinton", "Sanders")

#transform string into date
df$Date = as.Date(substr(df[, 1], 9, 18))

#set up two data frames:
#df2 has the delegates won per candidate and per date
#df3 has the cumulatative delegate count per candidate and per date
df2 <- data.frame(Date=unique(df$Date))
df3 <- df2
for (i in 3:dim(df)[2]) {
  df[, i] = as.numeric(df[, i])
  df2[[names(df)[i]]] = rep(NA, length(df2$Date))
  df3[[names(df)[i]]] = rep(NA, length(df2$Date))
  for (j in 1:length(df2$Date)) {
    df2[[names(df)[i]]][j] = sum(subset(df, Date==df2$Date[j])[[names(df)[i]]])
    df3[[names(df)[i]]][j] = sum(df2[[names(df)[i]]][1:j])
  }
}

df2$Total = rowSums(df2[,2:3], na.rm=TRUE)
df3$Total = rowSums(df3[,2:3], na.rm=TRUE)

#reshape data so that candidate becomes a variable
mdata <- melt(df2, id=c("Date", "Total"))
mdata2 <- melt(df3, id=c("Date", "Total"))
names(mdata) <- c("Date", "Total", "Candidate", "Delegates")
mdata[["Cumulative"]] <- mdata2[, 4]
mdata[["CTotal"]] <- mdata2[, 2]

results = na.omit(mdata)

#colors
colors <- c("#D4AA00", "#228b22")
labels = c("Clinton", "Sanders")

d = ggplot(results, aes(x=Date, y=Cumulative/CTotal, fill=Candidate, colour=Candidate)) + 
  geom_area() + 
  scale_color_manual(values=colors, labels=labels) + 
  scale_fill_manual(values=colors, labels=labels) + 
  scale_y_continuous(labels=percent) + 
  labs(x="Date", y="% Pledged delegates", title="Share of delegates",
       fill="Candidate", colour="Candidate")

#display plot
d

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current15:19, 6 April 2016Thumbnail for version as of 15:19, 6 April 2016484 × 333 (76 KB)Χ (talk | contribs)User created page with UploadWizard

There are no pages that use this file.

Metadata