File:Trump vs. Sanders nationwide.svg

Original file(SVG file, nominally 810 × 360 pixels, file size: 147 KB)

Captions

Captions

Add a one-line explanation of what this file represents

Summary

edit
Description
English: A combination of nationwide opinion polls of Sanders vs. Trump 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=3)

table = table[, c(1, 2, 8, 3:6)]

names(table) <- c("Source", "Date", "Size", "DC", "DP", "RC", "RP")

df = table[which(table$DC=="Bernie Sanders" & table$RC=="Donald Trump"), c(1:3, 5, 7)]

names(df)[4:5] <- c("Sanders", "Trump")

for (i in 4: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 = as.numeric(sub(",", "", df$Size))

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

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

colors = c("#3333FF", "#FF3333")
labels = c("Sanders", "Trump")

results = mdata

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", "Sanders vs. Trump"))) + 
  scale_size_area(max_size=3,
                  labels=function(x) x^2,
                  name="Sample Size") + 
  scale_y_continuous(breaks=seq(0,1,0.05), minor_breaks=seq(0,1,0.01), labels=percent,
                     limits=c(0.35, 0.6)) + 
  scale_x_date(labels=date_format("%b %d"),
               breaks=sort(c(seq(as.Date("2016/1/1"), as.Date("2016/10/1"), "month"),
                             as.Date("2016/11/8"))),
               limits=c(as.Date("2016/1/1"), as.Date("2016/11/8"))) +
  theme(panel.grid.minor=element_line(size=0.2),
        panel.grid.major=element_line(size=0.6))

#save plot as "st.svg"
svg(filename="st.svg", 
    width=9, 
    height=4, 
    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
current04:37, 30 May 2016Thumbnail for version as of 04:37, 30 May 2016810 × 360 (147 KB)Χ (talk | contribs)tweak
15:21, 26 May 2016Thumbnail for version as of 15:21, 26 May 2016810 × 360 (149 KB)Χ (talk | contribs)update
04:02, 24 May 2016Thumbnail for version as of 04:02, 24 May 2016810 × 360 (150 KB)Χ (talk | contribs)date range
03:34, 24 May 2016Thumbnail for version as of 03:34, 24 May 2016810 × 360 (142 KB)Χ (talk | contribs)User created page with UploadWizard

There are no pages that use this file.

Metadata