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")
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)
An example of a single question item:
questions <- pollster_questions() questions$content$items[[1]]
An example of a single chart item:
charts <- pollster_charts() charts$content$items[[1]]
An example of a single poll item:
polls <- pollster_polls() polls$content$items[[1]]
Get all available tags for use as the tag
parameter with charts, polls, and questions:
map_chr(pollster_tags()$content, "slug")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.