File:Nationwide polls for the 2016 Republican Party presidential primaries.svg
From WikiProjectMed
Jump to navigation
Jump to search
Size of this PNG preview of this SVG file: 800 × 356 pixels. Other resolutions: 320 × 142 pixels | 640 × 284 pixels | 1,024 × 455 pixels | 1,280 × 569 pixels | 2,560 × 1,138 pixels | 810 × 360 pixels.
Original file (SVG file, nominally 810 × 360 pixels, file size: 312 KB)
This file is from a shared repository and may be used by other projects. The description on its file description page there is shown below.
Summary
DescriptionNationwide polls for the 2016 Republican Party presidential primaries.svg |
English: A combination of nationwide opinion polls during the 2016 Republican Party presidential primaries. The trend lines are Local Regressions with α = 0.8 and 95% confidence interval ribbons. The point sizes and trend lines are weighted according to the margin of error of each poll. |
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, largely inspired by this file.
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/Nationwide_opinion_polling_for_the_Republican_Party_2016_presidential_primaries", ssl.verifyPeer=FALSE)
tables <- c()
for (i in 1:7) {
tables[[i]] <- htmltab(theurl, which=i+2)
}
for (name in names(tables[[7]])) {
for (j in 1:6) {
if (!(name %in% names(tables[[j]]))) {
tables[[j]][[name]] = rep(NA, nrow(tables[[j]]))
}
}
}
df = do.call(rbind, tables)[,c(2,4,7,5,6,16)]
names(df) <- c("Size", "Date", "Trump", "Cruz", "Kasich", "Rubio")
df$Date = sub("[0-9]+\\s*–\\s*([0-9]+)", "\\1", df$Date)
df$Date = sub(".*–", "", df$Date)
df$Date = sub("[0-9]+\\s*-\\s*([0-9]+)", "\\1", df$Date)
df$Date = sub(".*-", "", df$Date)
df$Date = trimws(df$Date)
df$Date = as.Date(df$Date, format="%B %d, %Y")
df$Size = as.numeric(sub(",", "", df$Size))
for (i in c(3:8)) {
df[[i]] = as.numeric(sub("%", "", df[[i]]))/100
}
df$Error = 1/sqrt(df$Size)
mdata <- melt(df, id=c("Date", "Error", "Size"))
names(mdata)[4:5] = c("Candidate", "Support")
colors = c("#283681", "#DAA520", "#29AB87", "#C60E3B")
labels = c("Trump", "Cruz", "Kasich", "Rubio")
results = mdata
#breaks() returns n evenly spaced numbers between x and y
#whose squares are divisible by p
#the function is used for the legend
breaks <- function(x, y, n, p) {
x = sqrt(ceiling(as.integer(x^2) / p) * p)
y = sqrt(floor(as.integer(y^2) / p) * p)
s = seq(x, y, length.out=n)
for (i in 2:(n-1)) {
s[i] = sqrt(round(s[i]^2 / p) * p)
}
return(unique(s))
}
d = ggplot(results, aes(x=Date, y=Support, colour=Candidate, size=1/Error, weight=1/Error)) +
geom_point(alpha=0.7) +
geom_smooth(span=0.8, show.legend=F) +
scale_colour_manual(values = colors) +
labs(title="Nationwide opinion polling for the 2016 Republican Party primaries") +
scale_size_area(max_size=3,
breaks=function(x) breaks(x[1], x[2], 5, 100), #5 numbers divisible by 100
labels=function(x) comma_format()(x^2),
name="Sample Size") +
scale_y_continuous(breaks=seq(0,0.7,0.1), minor_breaks=seq(0,0.66,0.02), labels=percent) +
scale_x_date(labels=date_format("%b %d"),
breaks=sort(c(seq(as.Date("2016/1/1"), as.Date("2016/7/21"), "month"),
seq(as.Date("2016/1/15"), as.Date("2016/7/1"), "month"),
as.Date("2016/7/21"))),
limits=c(as.Date("2016/1/1"), as.Date("2016/7/21"))) +
theme(panel.grid.minor=element_line(size=0.2),
panel.grid.major=element_line(size=0.6))
#save plot as "rp.svg"
svg(filename="rp.svg",
width=9,
height=4,
pointsize=12,
bg="transparent")
d
dev.off()
Items portrayed in this file
depicts
7 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 | 03:45, 31 May 2016 | 810 × 360 (312 KB) | commons>Χ | better legend for sample size |
File usage
There are no pages that use this file.
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 | 648pt |
---|---|
Height | 288pt |