File:Delegate share for the 2016 Republican Party presidential primaries.svg
Page contents not supported in other languages.
Tools
Actions
General
In other projects
Appearance
Size of this PNG preview of this SVG file: 484 × 333 pixels. Other resolutions: 320 × 220 pixels | 640 × 440 pixels | 1,024 × 705 pixels | 1,280 × 881 pixels | 2,560 × 1,761 pixels.
Original file (SVG file, nominally 484 × 333 pixels, file size: 83 KB)
This is a file from the Wikimedia Commons. Information from its description page there is shown below. Commons is a freely licensed media file repository. You can help. |
Summary
DescriptionDelegate share for the 2016 Republican Party presidential primaries.svg |
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
I, the copyright holder of this work, hereby publish it under the following license:
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
The graph is generated by the following R script:
library(RCurl)
library(reshape)
library(htmltab)
library(ggplot2)
library(stringr)
library(scales)
#get the table from the url
theurl <- getURL("https://en.wikipedia.org/wiki/Results_of_the_Republican_Party_presidential_primaries,_2016", ssl.verifyPeer=FALSE)
table <- htmltab(theurl, which=7)
#keep only the useful columns and name them
df <- table[3:61, 1:ncol(table)-1]
names(df)[1:2] <- c("Date", "State")
#extract delegate count
for (i in 3:ncol(df)) {
df[[i]] = str_extract_all(df[[i]], "[0-9,]+ delegate")
df[[i]] = as.numeric(str_extract(df[[i]], "[0-9,]+"))
}
#sum up minor candidates
df[[7]] = rowSums(df[,7:14], na.rm=TRUE)
names(df)[7] = "Others"
df = df[,1:7]
#extract state names
df[[2]] = str_replace(df[[2]], "Binding[A-Za-z ]+", "")
df[[2]] = str_replace(df[[2]], "Territorial[A-Za-z ]+", "")
df[[2]] = str_replace(df[[2]], "State[A-Za-z ]+", "")
df[[2]] = str_replace(df[[2]], "District Co[A-Za-z ]+", "")
df[[2]] = str_replace(df[[2]], "Conv[A-Za-z ]+", "")
#extract date
df$Date = as.Date(df$Date, format="%b %d")
#replace NA witht 0
df[is.na(df)] = 0
#remove contests that haven't happened
df = df[rowSums(df[,3:7], na.rm=TRUE)!=0,]
#new dataframes for unique dates and cumulative count
df2 = data.frame(Date=unique(df$Date))
df3 = df2
for (i in 3:7) {
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:6], na.rm=TRUE)
df3$Total = rowSums(df3[,2:6], 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 = mdata
#colors
colors <- c("#283681", "#DAA520", "#29AB87", "#C60E3B", "#000000")
labels = c("Trump", "Cruz", "Kasich", "Rubio", "Other")
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="% Delegates", title="Share of delegates",
fill="Candidate", colour="Candidate")
#display plot
d
Items portrayed in this file
depicts
4 April 2016
File history
Click on a date/time to view the file as it appeared at that time.
Date/Time | Thumbnail | Dimensions | User | Comment | |
---|---|---|---|---|---|
current | 13:50, 6 April 2016 | 484 × 333 (83 KB) | Χ | update | |
20:37, 4 April 2016 | 484 × 333 (81 KB) | Χ | User created page with UploadWizard |
File usage
No pages on the English Wikipedia use this file (pages on other projects are not listed).
Metadata
This file contains additional information, probably added from the digital camera or scanner used to create or digitize it.
If the file has been modified from its original state, some details may not fully reflect the modified file.
Width | 387pt |
---|---|
Height | 266pt |