PollstR Usage

knitr::opts_chunk$set(echo = TRUE)

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]]

Charts

An example of a single chart item:

charts <- pollster_charts()
charts$content$items[[1]]

Polls

An example of a single poll item:

polls <- pollster_polls()
polls$content$items[[1]]

Tags

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

map_chr(pollster_tags()$content, "slug")


Try the pollstR package in your browser

Any scripts or data that you put into this service are public.

pollstR documentation built on May 1, 2019, 10:09 p.m.