PollstR Usage

Jeffrey Arnold

This document provides a few examples that use the pollstR package.

library("pollstR")
library("ggplot2")
library("dplyr")
library("tidyr")
library("purrr")

Produce a Plot of Chart

The pollstR package can be used to reproduce plots in the charts. This will use the Donald Trump favorable rating as the example. This chart has the slug donald-trump-favorable-rating

slug <- "donald-trump-favorable-rating"
polls <- pollster_charts_polls(slug)[["content"]]
trendlines <- pollster_charts_trendlines(slug)[["content"]]

Plot the points for each response (Favorable, Unfavorable, and Undecided) and fit a loess smoother through them:

.data <- gather(polls, response, value,
                Favorable, Unfavorable, Undecided) %>%
  mutate(value = if_else(is.na(value), 0, value))
ggplot() +
  geom_point(data = .data,
             aes(x = end_date, y = value, color = response),
             alpha = 0.5) +
  geom_smooth(data = .data, method = "loess", se = FALSE,
              aes(x = end_date, y = value, color = response))

Plot the points with the Pollster trendlines:

ggplot() +
  geom_point(data = .data,
             mapping = aes(x = end_date, y = value, color = response),
             alpha = 0.5) +
  geom_line(data = trendlines,
            mapping = aes(x = date, y = value, color = label), size = 1.5)

For each of the subpopulations (Likely Voter, Registered Voters, Adults), plot the favorability rating:

ggplot(data = filter(.data, response == "Favorable"),
       mapping = aes(x = end_date, y = value, color = sample_subpopulation)) +
  geom_point(alpha = 0.5) +
  geom_smooth(method = "loess", se = FALSE)

Questions

An example of a single question item:

questions <- pollster_questions()
questions$content$items[[1]]
## $slug
## [1] "18-AZ-Gov-GE-DvF"
## 
## $name
## [1] "2018 Arizona Gubernatorial GE"
## 
## $tags
## list()
## 
## $charts
## list()
## 
## $election_date
## [1] "2018-11-06"
## 
## $n_polls
## [1] 1
## 
## $created_at
## [1] "2017-06-13T13:32:26.000Z"
## 
## $responses
## $responses[[1]]
## $responses[[1]]$label
## [1] "Ducey"
## 
## $responses[[1]]$name
## [1] "Doug Ducey"
## 
## $responses[[1]]$party
## [1] "Republican"
## 
## $responses[[1]]$incumbent
## [1] TRUE
## 
## 
## $responses[[2]]
## $responses[[2]]$label
## [1] "Farley"
## 
## $responses[[2]]$name
## [1] "Steve Farley"
## 
## $responses[[2]]$party
## [1] "Democrat"
## 
## $responses[[2]]$incumbent
## [1] FALSE
## 
## 
## $responses[[3]]
## $responses[[3]]$label
## [1] "Other"
## 
## $responses[[3]]$name
## [1] "Other"
## 
## $responses[[3]]$party
## NULL
## 
## $responses[[3]]$incumbent
## [1] FALSE
## 
## 
## $responses[[4]]
## $responses[[4]]$label
## [1] "Undecided"
## 
## $responses[[4]]$name
## [1] "Undecided"
## 
## $responses[[4]]$party
## NULL
## 
## $responses[[4]]$incumbent
## [1] FALSE

Charts

An example of a single chart item:

charts <- pollster_charts()
charts$content$items[[1]]
## $slug
## [1] "2017-new-jersey-governor-republican-primary"
## 
## $title
## [1] "2017 New Jersey Governor Republican Primary"
## 
## $url
## [1] "http://elections.huffingtonpost.com/pollster/2017-new-jersey-governor-republican-primary"
## 
## $created_at
## [1] "2017-05-25T17:27:08.000Z"
## 
## $tags
## list()
## 
## $pollster_estimates
## list()
## 
## $pollster_estimate_summary
## NULL
## 
## $question
## $question$slug
## [1] "17-NJ-Gov-RepPR"
## 
## $question$name
## [1] "2017 New Jersey Gubernatorial Republican Primary"
## 
## $question$tags
## list()
## 
## $question$charts
## $question$charts[[1]]
## [1] "2017-new-jersey-governor-republican-primary"
## 
## 
## $question$election_date
## NULL
## 
## $question$n_polls
## [1] 4
## 
## $question$created_at
## [1] "2017-05-03T15:05:52.000Z"
## 
## $question$responses
## $question$responses[[1]]
## $question$responses[[1]]$label
## [1] "Guadagno"
## 
## $question$responses[[1]]$name
## [1] "Kim Guadagno"
## 
## $question$responses[[1]]$party
## [1] "Republican"
## 
## $question$responses[[1]]$incumbent
## [1] FALSE
## 
## 
## $question$responses[[2]]
## $question$responses[[2]]$label
## [1] "Ciattarelli"
## 
## $question$responses[[2]]$name
## [1] "Jack Ciattarelli"
## 
## $question$responses[[2]]$party
## [1] "Republican"
## 
## $question$responses[[2]]$incumbent
## [1] FALSE
## 
## 
## $question$responses[[3]]
## $question$responses[[3]]$label
## [1] "Rogers"
## 
## $question$responses[[3]]$name
## [1] "Steve Rogers"
## 
## $question$responses[[3]]$party
## [1] "Republican"
## 
## $question$responses[[3]]$incumbent
## [1] FALSE
## 
## 
## $question$responses[[4]]
## $question$responses[[4]]$label
## [1] "Other"
## 
## $question$responses[[4]]$name
## [1] "Other"
## 
## $question$responses[[4]]$party
## NULL
## 
## $question$responses[[4]]$incumbent
## [1] FALSE
## 
## 
## $question$responses[[5]]
## $question$responses[[5]]$label
## [1] "Undecided"
## 
## $question$responses[[5]]$name
## [1] "Undecided"
## 
## $question$responses[[5]]$party
## NULL
## 
## $question$responses[[5]]$incumbent
## [1] FALSE

Polls

An example of a single poll item:

polls <- pollster_polls()
polls$content$items[[1]]
## $slug
## [1] "surveymonkey-28547"
## 
## $start_date
## [1] "2018-03-22"
## 
## $end_date
## [1] "2018-03-28"
## 
## $created_at
## [1] "2018-04-02T22:15:27.000Z"
## 
## $updated_at
## [1] "2018-04-02T18:15:27.000Z"
## 
## $survey_house
## [1] "SurveyMonkey"
## 
## $mode
## [1] "Internet"
## 
## $url
## [1] "https://drive.google.com/file/d/1D5LtwhhzowopN7ZwlnU5lUN6a7k7oPOA/view"
## 
## $partisanship
## [1] "Nonpartisan"
## 
## $partisan_affiliation
## [1] "None"
## 
## $poll_questions
## $poll_questions[[1]]
## $poll_questions[[1]]$text
## [1] ""
## 
## $poll_questions[[1]]$question
## $poll_questions[[1]]$question$slug
## [1] "00c -Pres-45-Trump - Job Approval - National"
## 
## $poll_questions[[1]]$question$name
## [1] "Trump, Donald - USPres - Job Approval"
## 
## $poll_questions[[1]]$question$tags
## list()
## 
## $poll_questions[[1]]$question$charts
## $poll_questions[[1]]$question$charts[[1]]
## [1] "trump-job-approval"
## 
## 
## $poll_questions[[1]]$question$election_date
## NULL
## 
## $poll_questions[[1]]$question$n_polls
## [1] 1102
## 
## $poll_questions[[1]]$question$created_at
## [1] "2017-01-10T18:32:55.000Z"
## 
## $poll_questions[[1]]$question$responses
## $poll_questions[[1]]$question$responses[[1]]
## $poll_questions[[1]]$question$responses[[1]]$label
## [1] "Approve"
## 
## $poll_questions[[1]]$question$responses[[1]]$name
## [1] "Approve"
## 
## $poll_questions[[1]]$question$responses[[1]]$party
## NULL
## 
## $poll_questions[[1]]$question$responses[[1]]$incumbent
## [1] FALSE
## 
## 
## $poll_questions[[1]]$question$responses[[2]]
## $poll_questions[[1]]$question$responses[[2]]$label
## [1] "Disapprove"
## 
## $poll_questions[[1]]$question$responses[[2]]$name
## [1] "Disapprove"
## 
## $poll_questions[[1]]$question$responses[[2]]$party
## NULL
## 
## $poll_questions[[1]]$question$responses[[2]]$incumbent
## [1] FALSE
## 
## 
## $poll_questions[[1]]$question$responses[[3]]
## $poll_questions[[1]]$question$responses[[3]]$label
## [1] "Undecided"
## 
## $poll_questions[[1]]$question$responses[[3]]$name
## [1] "Undecided"
## 
## $poll_questions[[1]]$question$responses[[3]]$party
## NULL
## 
## $poll_questions[[1]]$question$responses[[3]]$incumbent
## [1] FALSE
## 
## 
## 
## 
## $poll_questions[[1]]$sample_subpopulations
## $poll_questions[[1]]$sample_subpopulations[[1]]
## $poll_questions[[1]]$sample_subpopulations[[1]]$name
## [1] "Adults"
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$observations
## [1] 55372
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$margin_of_error
## [1] 1
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[1]]
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[1]]$text
## [1] "Strongly Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[1]]$pollster_label
## [1] "Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[1]]$value
## [1] 23
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[2]]
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[2]]$text
## [1] "Somewhat Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[2]]$pollster_label
## [1] "Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[2]]$value
## [1] 20
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[3]]
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[3]]$text
## [1] "Somewhat Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[3]]$pollster_label
## [1] "Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[3]]$value
## [1] 12
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[4]]
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[4]]$text
## [1] "Strongly Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[4]]$pollster_label
## [1] "Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[4]]$value
## [1] 43
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[5]]
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[5]]$text
## [1] "No answer"
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[5]]$pollster_label
## [1] "Undecided"
## 
## $poll_questions[[1]]$sample_subpopulations[[1]]$responses[[5]]$value
## [1] 2
## 
## 
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]
## $poll_questions[[1]]$sample_subpopulations[[2]]$name
## [1] "Adults - Democrat"
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$observations
## [1] 6133
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$margin_of_error
## NULL
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[1]]
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[1]]$text
## [1] "Strongly Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[1]]$pollster_label
## [1] "Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[1]]$value
## [1] 2
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[2]]
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[2]]$text
## [1] "Somewhat Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[2]]$pollster_label
## [1] "Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[2]]$value
## [1] 5
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[3]]
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[3]]$text
## [1] "Somewhat Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[3]]$pollster_label
## [1] "Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[3]]$value
## [1] 11
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[4]]
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[4]]$text
## [1] "Strongly Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[4]]$pollster_label
## [1] "Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[4]]$value
## [1] 82
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[5]]
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[5]]$text
## [1] "No answer"
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[5]]$pollster_label
## [1] "Undecided"
## 
## $poll_questions[[1]]$sample_subpopulations[[2]]$responses[[5]]$value
## [1] 0
## 
## 
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]
## $poll_questions[[1]]$sample_subpopulations[[3]]$name
## [1] "Adults - Republican"
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$observations
## [1] 5669
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$margin_of_error
## NULL
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[1]]
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[1]]$text
## [1] "Strongly Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[1]]$pollster_label
## [1] "Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[1]]$value
## [1] 52
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[2]]
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[2]]$text
## [1] "Somewhat Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[2]]$pollster_label
## [1] "Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[2]]$value
## [1] 34
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[3]]
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[3]]$text
## [1] "Somewhat Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[3]]$pollster_label
## [1] "Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[3]]$value
## [1] 8
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[4]]
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[4]]$text
## [1] "Strongly Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[4]]$pollster_label
## [1] "Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[4]]$value
## [1] 6
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[5]]
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[5]]$text
## [1] "No answer"
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[5]]$pollster_label
## [1] "Undecided"
## 
## $poll_questions[[1]]$sample_subpopulations[[3]]$responses[[5]]$value
## [1] 0
## 
## 
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]
## $poll_questions[[1]]$sample_subpopulations[[4]]$name
## [1] "Adults - independent"
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$observations
## [1] 2322
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$margin_of_error
## NULL
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[1]]
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[1]]$text
## [1] "Strongly Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[1]]$pollster_label
## [1] "Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[1]]$value
## [1] 10
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[2]]
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[2]]$text
## [1] "Somewhat Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[2]]$pollster_label
## [1] "Approve"
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[2]]$value
## [1] 26
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[3]]
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[3]]$text
## [1] "Somewhat Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[3]]$pollster_label
## [1] "Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[3]]$value
## [1] 23
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[4]]
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[4]]$text
## [1] "Strongly Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[4]]$pollster_label
## [1] "Disapprove"
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[4]]$value
## [1] 40
## 
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[5]]
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[5]]$text
## [1] "No answer"
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[5]]$pollster_label
## [1] "Undecided"
## 
## $poll_questions[[1]]$sample_subpopulations[[4]]$responses[[5]]$value
## [1] 2

Tags

Get all available tags for use as the tag parameter with charts, polls, and questions:

map_chr(pollster_tags()$content, "slug")
##  [1] "2012-president-gop-primary" "2012-president"            
##  [3] "favorable-ratings"          "obama-job-approval"        
##  [5] "2012-senate"                "2012-house"                
##  [7] "2012-governor"              "2012-governor-dem-primary" 
##  [9] "2012-senate-dem-primary"    "2012-senate-gop-primary"   
## [11] "2012-governor-gop-primary"  "2014-house"                
## [13] "2013-governor"              "2013-senate"               
## [15] "2014-senate"                "2013-senate-dem-primary"   
## [17] "2014-governor"              "2016-president-gop-primary"
## [19] "2013-house"                 "2016-president-dem-primary"
## [21] "2013-senate-gop-primary"    "2016-president"            
## [23] "2014-senate-gop-primary"    "2014-senate-dem-primary"   
## [25] "2014-governor-dem-primary"  "2014-senate-primary"       
## [27] "2014-governor-gop-primary"  "2016-senate"               
## [29] "2016-governor"              "2016-senate-dem-primary"   
## [31] "2016-senate-gop-primary"    "2016-house"                
## [33] "uk-eu-referendum"           "2016-governor-dem-primary" 
## [35] "2016-governor-gop-primary"