Load required packages:
library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0 ✔ purrr 0.2.5
## ✔ tibble 2.0.0 ✔ dplyr 0.7.8
## ✔ tidyr 0.8.2 ✔ stringr 1.3.1
## ✔ readr 1.3.1 ✔ forcats 0.3.0
## ── Conflicts ─────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(pageviews) # This package gets data on Wikipedia viewing
library(DT) # DT stands for datatable, and creates interactive tables
Get the views of the Gun control Wikipedia article.
guns <- article_pageviews(article = "Gun control", start = as.Date("2015-7-1"), end = as.Date("2018-12-31"))
Graph it over time:
guns %>%
ggplot(aes(x = date, y = views)) +
geom_line(color = "blue") +
theme_minimal() +
labs(x = "Date",
y = "Views",
title = "Views of the Wikipedia Gun control article over time")
guns %>%
select(date, views) %>%
arrange(-views) %>%
datatable()
Wikipedia views the day after the Las Vegas shooting.
top_articles(start = as.Date("2017-10-2")) %>%
select(article, views) %>%
filter(!article == "Main_Page", !article == "Special:Search") %>%
datatable()
Wikipedia views the day after the Florida school shooting.
top_articles(start = as.Date("2018-2-15")) %>%
select(article, views) %>%
filter(!article == "Main_Page", !article == "Special:Search") %>%
datatable()
Getting the data.
vegas_shooting <- article_pageviews(article = "Gun_control",
start = as.Date("2017-9-24"),
end = as.Date("2017-10-15"))
vegas_shooting <- vegas_shooting %>%
mutate(day = -7:14) %>%
mutate(event = "Las Vegas")
florida_shooting <- article_pageviews(article = "Gun_control",
start = as.Date("2018-2-7"),
end = as.Date("2018-2-28"))
florida_shooting <- florida_shooting %>%
mutate(day = -7:14) %>%
mutate(event = "Florida")
shootings <- bind_rows(vegas_shooting, florida_shooting)
Comparing the two shootings.
shootings %>%
ggplot(aes(x = day, y = views, color = event)) +
geom_line() +
theme_minimal() +
labs(x= "Days before/after shooting",
y = "Views",
title = "Views of the Wikipedia Gun control article, before/after two mass shootings")