1 + 1[1] 2
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
1 + 1[1] 2
You can add options to executable code like this
[1] 4
The echo: false option disables the printing of code (only output is displayed).
#install.packages("pageviews_0.5.0.tar", repos = NULL, type = "source")
devtools::install_github("ironholds/pageviews")WARNING: Rtools is required to build R packages, but is not currently installed.
Please download and install Rtools 4.2 from https://cran.r-project.org/bin/windows/Rtools/.
Skipping install of 'pageviews' from a github remote, the SHA1 (d32c629c) has not changed since last install.
Use `force = TRUE` to force installation
force = TRUElibrary(tidyverse)Warning: package 'tidyverse' was built under R version 4.2.3
Warning: package 'ggplot2' was built under R version 4.2.3
Warning: package 'tibble' was built under R version 4.2.3
Warning: package 'tidyr' was built under R version 4.2.3
Warning: package 'readr' was built under R version 4.2.3
Warning: package 'purrr' was built under R version 4.2.3
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'stringr' was built under R version 4.2.3
Warning: package 'forcats' was built under R version 4.2.3
Warning: package 'lubridate' was built under R version 4.2.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(pageviews) # This package gets data on Wikipedia viewing
library(DT) # DT stands for datatable, and creates interactive tablesWarning: package 'DT' was built under R version 4.2.3
library(infer) # for some stats like t_testWarning: package 'infer' was built under R version 4.2.3
library(devtools)Warning: package 'devtools' was built under R version 4.2.3
Loading required package: usethis
Warning: package 'usethis' was built under R version 4.2.3
mass_shootings <- article_pageviews(article = "Mass_shootings_in_the_United_States", start = as.Date("2017-1-1"), end = as.Date("2023-12-31"))
glimpse(mass_shootings)Rows: 2,554
Columns: 8
$ project <chr> "wikipedia", "wikipedia", "wikipedia", "wikipedia", "wikip…
$ language <chr> "en", "en", "en", "en", "en", "en", "en", "en", "en", "en"…
$ article <chr> "Mass_shootings_in_the_United_States", "Mass_shootings_in_…
$ access <chr> "all-access", "all-access", "all-access", "all-access", "a…
$ agent <chr> "all-agents", "all-agents", "all-agents", "all-agents", "a…
$ granularity <chr> "daily", "daily", "daily", "daily", "daily", "daily", "dai…
$ date <dttm> 2017-01-01, 2017-01-02, 2017-01-03, 2017-01-04, 2017-01-0…
$ views <dbl> 13, 9, 7, 13, 21, 54, 49, 32, 16, 19, 24, 14, 9, 11, 6, 21…
mass_shootings |>
ggplot(aes(x = date, y = views)) +
geom_line()This graph plots the amount of views for mass shootings around the United States that were searched
gun_control <- article_pageviews(article = "Gun control", start = as.Date("2017-1-1"), end = as.Date("2023-12-31"))
glimpse(gun_control)Rows: 2,556
Columns: 8
$ project <chr> "wikipedia", "wikipedia", "wikipedia", "wikipedia", "wikip…
$ language <chr> "en", "en", "en", "en", "en", "en", "en", "en", "en", "en"…
$ article <chr> "Gun_control", "Gun_control", "Gun_control", "Gun_control"…
$ access <chr> "all-access", "all-access", "all-access", "all-access", "a…
$ agent <chr> "all-agents", "all-agents", "all-agents", "all-agents", "a…
$ granularity <chr> "daily", "daily", "daily", "daily", "daily", "daily", "dai…
$ date <dttm> 2017-01-01, 2017-01-02, 2017-01-03, 2017-01-04, 2017-01-0…
$ views <dbl> 285, 393, 434, 544, 635, 620, 431, 464, 610, 595, 686, 688…
gun_control |>
ggplot(aes(x = date, y = views)) +
geom_line()This graph plots the amount of views for gun control that were searched
mass_shootings |>
slice_max(views, n = 10) project language article access agent
1 wikipedia en Mass_shootings_in_the_United_States all-access all-agents
2 wikipedia en Mass_shootings_in_the_United_States all-access all-agents
3 wikipedia en Mass_shootings_in_the_United_States all-access all-agents
4 wikipedia en Mass_shootings_in_the_United_States all-access all-agents
5 wikipedia en Mass_shootings_in_the_United_States all-access all-agents
6 wikipedia en Mass_shootings_in_the_United_States all-access all-agents
7 wikipedia en Mass_shootings_in_the_United_States all-access all-agents
8 wikipedia en Mass_shootings_in_the_United_States all-access all-agents
9 wikipedia en Mass_shootings_in_the_United_States all-access all-agents
10 wikipedia en Mass_shootings_in_the_United_States all-access all-agents
granularity date views
1 daily 2022-05-25 224670
2 daily 2019-08-04 187025
3 daily 2019-08-05 140170
4 daily 2022-05-26 104011
5 daily 2019-08-06 100574
6 daily 2018-02-15 87552
7 daily 2023-10-26 83192
8 daily 2018-02-16 70186
9 daily 2022-05-27 68475
10 daily 2021-03-23 68186
gun_control |>
slice_max(views, n = 10) project language article access agent granularity date
1 wikipedia en Gun_control all-access all-agents daily 2022-05-25
2 wikipedia en Gun_control all-access all-agents daily 2018-02-16
3 wikipedia en Gun_control all-access all-agents daily 2018-02-15
4 wikipedia en Gun_control all-access all-agents daily 2022-05-26
5 wikipedia en Gun_control all-access all-agents daily 2018-02-22
6 wikipedia en Gun_control all-access all-agents daily 2017-10-03
7 wikipedia en Gun_control all-access all-agents daily 2018-02-20
8 wikipedia en Gun_control all-access all-agents daily 2018-02-19
9 wikipedia en Gun_control all-access all-agents daily 2018-02-21
10 wikipedia en Gun_control all-access all-agents daily 2018-02-18
views
1 9666
2 6549
3 6117
4 5587
5 5142
6 4342
7 4233
8 4229
9 3914
10 3760
guns <- bind_rows(gun_control, mass_shootings) guns |>
ggplot(aes(x = date, y = views, color = article)) +
geom_line()This graph plots the amount of views for mass shootings around the United States in comparison to the views for gun control that were searched. This graph shows that there is a slight increase in views of gun control articles when a mass shooting has happened in the United States. 2.
guns |>
pivot_wider(names_from = article, values_from = views) |>
ggplot(aes(x = Mass_shootings_in_the_United_States, y = Gun_control)) + # create scatterplot
geom_point() +
geom_smooth(method = lm) + # create regression line
labs(x = "Views of the Wikipedia Mass shootings in the US article",
y = "Views of the Wikipedia Gun violence article",
title = "Relationship between Wikipedia article views")`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
Warning: Removed 2 rows containing missing values (`geom_point()`).
This scatterplot shows the correlation between views of articles about Mass shootings in the US and articles about Gun violence. 3.
elpaso <- top_articles(start = as.Date("2019-8-4")) |>
filter(!article == "Main_Page", !article == "Special:Search")elpaso |>
select(article, views) |>
filter(!article == "Main_Page", !article == "Special:Search") |>
top_n(10, views) |>
ggplot(aes(x = fct_rev(as_factor(article)), y = views)) +
geom_col(fill = "purple") +
coord_flip()scale_y_continuous(labels = scales::comma)<ScaleContinuousPosition>
Range:
Limits: 0 -- 1
labs(y = "Number of Views", x = "Article", title = "Top Wikipedia articles, Aug. 4, 2019")$y
[1] "Number of Views"
$x
[1] "Article"
$title
[1] "Top Wikipedia articles, Aug. 4, 2019"
attr(,"class")
[1] "labels"
This bar graph shows the top ten views of articles the day of the El Paso mass shooting on August 4th, 2019.
florida <- top_articles(start = as.Date("2018-2-14")) |>
filter(!article == "Main_Page", !article == "Special:Search")florida |>
select(article, views) |>
filter(!article == "Main_Page", !article == "Special:Search") |>
top_n(10, views) |>
ggplot(aes(x = fct_rev(as_factor(article)), y = views)) +
geom_col(fill = "green") +
coord_flip()scale_y_continuous(labels = scales::comma)<ScaleContinuousPosition>
Range:
Limits: 0 -- 1
labs(y = "Number of Views", x = "Article", title = "Top Wikipedia articles, Feb. 14, 2018")$y
[1] "Number of Views"
$x
[1] "Article"
$title
[1] "Top Wikipedia articles, Feb. 14, 2018"
attr(,"class")
[1] "labels"
This bar graph shows the top ten article views the day of the Sandy Hook shooting in FL on February 14th 2018.
texas <- article_pageviews(article = "Gun_control",
start = as.Date("2019-7-21"),
end = as.Date("2019-8-18"))
texas <- texas |>
mutate(day = -14:14) |>
mutate(event = "Texas")
texas |>
ggplot(aes(x = day, y = views)) +
geom_line()This graph shows that 14 days prior to the mass shooting in El Paso mass shooting articles were not viewed often and the views decreased significantly about 6 days after the shooting.
florida <- article_pageviews(article = "Gun_control",
start = as.Date("2018-1-31"),
end = as.Date("2018-2-28"))
florida <- florida |>
mutate(day = -14:14) |>
mutate(event = "Florida")
florida |>
ggplot(aes(x = day, y = views)) +
geom_line()This graph shows that there were minimal views of mass shootings 14 days prior to the Sandy Hook shooting and a delay in the searches by about a day after the shooting and the searches slowly decreased with one more spike on about day 6.
shootings <- bind_rows(texas, florida)
shootings |>
ggplot(aes(x = day, y = views, color = event)) +
geom_line() +
theme_minimal() +
labs(x = "Days before/after Shooting",
y = "Wikipedia Views",
color = "Event",
title = "Views of the Wikipedia Gun Control Article before and after Two Mass Shootings")Accoording to this chart the views of mass shooting articles increased most with the Florida shooting with a delay in the searches and the Texas shooting had an immediate increase in views with about 4000 less views than the FL shooting. 5.
florida |>
mutate(after_event = (day > 0)) |>
t_test(views ~ after_event)Warning: The statistic is based on a difference or ratio; by default, for
difference-based statistics, the explanatory variable is subtracted in the
order "TRUE" - "FALSE", or divided in the order "TRUE" / "FALSE" for
ratio-based statistics. To specify this order yourself, supply `order =
c("TRUE", "FALSE")`.
# A tibble: 1 × 7
statistic t_df p_value alternative estimate lower_ci upper_ci
<dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl>
1 8.75 13.5 0.000000612 two.sided 3250. 2451. 4048.
florida |>
mutate(after_event = (day > 0)) |>
group_by(after_event) |>
summarize(Mean = mean(views),
StdDev = sd(views),
N = n())# A tibble: 2 × 4
after_event Mean StdDev N
<lgl> <dbl> <dbl> <int>
1 FALSE 577. 203. 15
2 TRUE 3827. 1375. 14
The average number of views of the Wikipedia Gun Control article in the 7 days prior to the two shootings (M = 577.13, SD = 202.83) was statistically significantly different from the average number of views in the 7 days after the shooting (M = 3826.71, SD = 1375.15), t(14.0) = 1.70, p = 6.12.