Load required packages:
library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0 ✔ purrr 0.3.0
## ✔ tibble 2.0.1 ✔ 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 articel.
guns <- article_pageviews(article = "Gun control", start = as.Date("2015-7-1"), end = as.Date("2018-12-31"))
guns %>%
ggplot(aes(x = date, y = views)) +
geom_line(color = "deepskyblue") +
theme_minimal() +
labs(x = "Date",
y = "Views",
titile = "Views of the Wikipedia Gun Control article over time")
guns %>%
select(date, views) %>%
arrange(-views) %>%
datatable(class = "cell-border stripe") %>%
formatStyle("date", backgroundColor = "lightcyan") %>%
formatStyle("views", backgroundColor = "lightgreen")
2017 Las Vegas Shooting:
top_articles(start = as.Date("2017-10-2")) %>%
select(article, views) %>%
filter(!article == "Main_Page", !article == "Special:Search") %>%
datatable(class = "cell-border stripe") %>%
formatStyle("article", backgroundColor = "lightyellow") %>%
formatStyle("views", backgroundColor = "lemonchiffon")
Stoneman Douglas High School Shooting:
top_articles(start = as.Date("2018-2-15")) %>%
select(article, views) %>%
filter(!article == "Main_Page", !article == "Special:Search") %>%
datatable(class = "cell-border stripe") %>%
formatStyle("article", backgroundColor = "lemonchiffon") %>%
formatStyle("views", backgroundColor = "lightyellow")
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", color = "Event", title = "Views of the Wikipedia Gun control article, befor/after two mass shootings")