File:US polls 2016 (three-way race).svg

Original file(SVG file, nominally 810 × 450 pixels, file size: 168 KB)

Captions

Captions

Add a one-line explanation of what this file represents

Summary edit

Description
English: A combination of nationwide opinion polls during the 2016 U.S. presidential election. 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 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, 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_United_States_presidential_election,_2016", ssl.verifyPeer=FALSE)
table <- htmltab(theurl, which=8)

table = table[, c(1:8)]

names(table) <- c("Size", "Date", "DC", "DP", "RC", "RP", "LC", "LP")
table$LC = sub(" \\(.*\\)", "", table$LC)

df = table[which(table$DC=="Hillary Clinton" &
                   table$RC=="Donald Trump" &
                   table$LC=="Gary Johnson"), c(1,2,4,6,8)]

df = df[1:(nrow(df)-1),] #remove oldest poll because it is too far from the others

names(df)[3:5] <- c("Clinton", "Trump", "Johnson")

for (i in 3:5) {
  df[[i]] = as.numeric(sub("%", "", df[[i]]))/100
}

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 = sub(".*Sample size: ([0-9,]+).*", "\\1", df$Size)
df$Size = as.numeric(sub(",", "", df$Size))

df$Error = 1/sqrt(df$Size)
mdata <- melt(df, id=c("Date", "Error", "Size"))

names(mdata)[4:5] = c("Candidate", "Support")

colors = c("#3333FF", "#FF3333", "#FED105")
labels = c("Clinton", "Trump", "Johnson")

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=expression(atop("Nationwide opinion polling for the 2016 U.S. presidential election", "Three-way race"))) + 
  scale_size_area(max_size=3,
                  breaks=function(x) breaks(x[1], x[2], 5, 250), #5 numbers divisible by 250
                  labels=function(x) comma_format()(x^2),
                  name="Sample Size") + 
  scale_y_continuous(breaks=seq(0,1,0.1), minor_breaks=seq(0,1,0.02), labels=percent,
                     limits=c(0, 0.5)) + 
  scale_x_date(labels=date_format("%b %d"),
               breaks=sort(c(seq(as.Date("2016/1/1"), as.Date("2016/10/1"), "week"),
                             as.Date("2016/11/8"))),
               minor_breaks=date_breaks("days")) +
  theme(panel.grid.minor=element_line(size=0.2),
        panel.grid.major=element_line(size=0.6))

#save plot as "ct.svg"
svg(filename="us2016-threeway.svg", 
    width=9, 
    height=5, 
    pointsize=12,
    bg="transparent")
d
dev.off()

File history

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

Date/TimeThumbnailDimensionsUserComment
current19:13, 1 August 2016Thumbnail for version as of 19:13, 1 August 2016810 × 450 (168 KB)Χ (talk | contribs)update
11:07, 24 July 2016Thumbnail for version as of 11:07, 24 July 2016810 × 450 (158 KB)Χ (talk | contribs)User created page with UploadWizard

There are no pages that use this file.

Metadata