This is an example use of rtimes
from Gaurav Soodoku. Find the original code at GitHub.
This example is a smaller subset of the analysis in the original code, just looking at one phrase: Affirmative Action
install.packages("devtools")
devtools::install_github("ropengov/rtimes")
library("rtimes")
First, get an API key for the Article Search API at developer.nytimes.com/apps/register. Then set the key in R:
options(nytimes_as_key = '<your api key>')
Set dates
dates <- format(seq(as.Date("1851/1/1"), as.Date("2015/1/1"), by = "3 months"), "%Y%m%d")
Make a data.frame to put data in to
results <- data.frame(startDate=NA, endDate=NA, afam=NA)
Loop through dates
for (i in 1:(length(dates) - 1)) {
counts <- NA
counts <- as_search(q = "affirmative action",
begin_date = dates[i],
end_date = dates[i + 1])$meta[1]
results[i, ] <- c(dates[i], dates[i + 1], counts)
}
Load packages
library("lubridate")
library("ggplot2")
library("scales")
library("grid")
Convert dates to class date
results$startDate <- as.Date(as.character(results$startDate), format = "%Y%m%d")
Plot mentions of Affirmative Action
ggplot(results, aes(x = startDate, y = cr)) +
geom_point(width = 1, color = "#42C4C7") +
geom_smooth(method = "gam", formula = y ~ s(x), aes(group = 1),
size = 1, colour = "#777777", alpha = 0.05, se = FALSE) +
ylab("No. Articles Containing the phrase 'Affirmative Action'") +
xlab("Year") +
theme_bw() +
scale_x_date(breaks = pretty_breaks(n = 10)) +
theme(panel.grid.major.y = element_line(colour = "#e3e3e3", linetype = "dotted"),
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_line(colour = "#f7f7f7", linetype = "solid"),
panel.border = element_blank(),
legend.position = "bottom",
legend.key = element_blank(),
legend.key.width = unit(1,"cm"),
axis.title = element_text(size = 18),
axis.text = element_text(size = 16),
axis.ticks.y = element_blank(),
axis.line.x = element_line(colour = 'red', size = 3, linetype = 'dashed'),
axis.title.x = element_text(vjust = -1),
axis.title.y = element_text(vjust = 1),
plot.margin = unit(c(0,.5,.5,.5), "cm"))