R Code:
if (!require("tidyverse"))
install.packages("tidyverse")
if (!require("plotly"))
install.packages("plotly")
library(tidyverse)
library(plotly)
# Defining date range
startdate <- "20220214"
enddate <- "20241114"
# Defining query: Russia
query <- "(russia%20OR%20ukrain)"
# Building the volume dataframe
vp1 <- "https://api.gdeltproject.org/api/v2/tv/tv?query="
vp2 <- "%20market:%22National%22&mode=timelinevol&format=csv&datanorm=raw&startdatetime="
vp3 <- "000000&enddatetime="
vp4 <- "000000"
text_v_url <- paste0(vp1, query, vp2, startdate, vp3, enddate, vp4)
v_url <- URLencode(text_v_url)
v_url
Ukraine <- read_csv(v_url)
Ukraine <- Ukraine %>%
rename(Date = 1, Ukraine = 3)
# Defining query: Gaza
query <- "(gaza%20OR%20israel)"
# Building the volume dataframe
vp1 <- "https://api.gdeltproject.org/api/v2/tv/tv?query="
vp2 <- "%20market:%22National%22&mode=timelinevol&format=csv&datanorm=raw&startdatetime="
vp3 <- "000000&enddatetime="
vp4 <- "000000"
text_v_url <- paste0(vp1, query, vp2, startdate, vp3, enddate, vp4)
v_url <- URLencode(text_v_url)
v_url
Gaza <- read_csv(v_url)
Gaza <- Gaza %>%
rename(Date = 1, Gaza = 3)
AllData <- left_join(Ukraine, Gaza)
# Filter AllData for Fox News, CNN, and MSNBC
AllData <- AllData %>%
arrange(Date) %>%
filter(Series == "FOXNEWS" |
Series == "CNN" |
Series == "MSNBC")
# Add "WeekOf" variable to the data frame
if (!require("lubridate"))
install.packages("lubridate")
library(lubridate)
AllData$WeekOf <- round_date(AllData$Date,
unit = "week",
week_start = getOption("lubridate.week.start", 1))
# Add "Period" variable to data frame
AllData <- AllData %>%
mutate(Period = case_when(
(Date < ymd("2022-06-06")) ~ "Period 1",
(Date < ymd("2023-10-07")) ~ "Period 2",
(Date < ymd("2024-01-22")) ~ "Period 3",
TRUE ~ "Period 4"
))
### Total volume ###
UkrData <- AllData %>%
group_by(WeekOf) %>%
summarize (Volume = sum(Ukraine)) %>%
mutate(Topic = "Ukraine")
GazData <- AllData %>%
group_by(WeekOf) %>%
summarize (Volume = sum(Gaza)) %>%
mutate(Topic = "Gaza")
UkrGazData <- rbind(UkrData, GazData)
# Graph the group distributions and averages
Boxplot <- ggplot(AllData, aes(x = Period,
y = Ukraine)) +
geom_boxplot() +
stat_summary(fun=mean, geom="point", shape=20, size=3, color="red", fill="red") +
theme_minimal() +
labs(y = "Coverage (15-sec. clips)",
title = "Ukraine coverage volume by period, all outlets",
subtitle = "(Red dots show averages)")
Boxplot
##### Stacked volume chart ###############
### Total volume ###
UkrData <- AllData %>%
group_by(WeekOf) %>%
summarize (Volume = sum(Ukraine)) %>%
mutate(Topic = "Ukraine")
GazData <- AllData %>%
group_by(WeekOf) %>%
summarize (Volume = sum(Gaza)) %>%
mutate(Topic = "Gaza")
UkrGazData <- rbind(UkrData, GazData)
Figure <- ggplot(UkrGazData, aes(x = WeekOf, y = Volume, fill = Topic)) +
geom_area() +
ylab("Volume - total") +
xlab("Week") +
geom_vline(xintercept = as.numeric(as.Date("2022-06-06")), linetype = "longdash") +
geom_vline(xintercept = as.numeric(as.Date("2023-10-07")), linetype = "longdash") +
geom_vline(xintercept = as.numeric(as.Date("2024-01-22")), linetype = "longdash") +
annotate(
"text",
x = as.Date("2022-03-15"),
y = max(UkrGazData$Volume) * 1,
label = "Per. 1",
color = "black",
size = 2.5,
fontface = "bold") +
annotate(
"text",
x = as.Date("2023-03-15"),
y = max(UkrGazData$Volume) * 1,
label = "Per. 2",
color = "black",
size = 2.5,
fontface = "bold") +
annotate(
"text",
x = as.Date("2023-12-01"),
y = max(UkrGazData$Volume) * 1,
label = "Per. 3",
color = "black",
size = 2.5,
fontface = "bold") +
annotate(
"text",
x = as.Date("2024-07-15"),
y = max(UkrGazData$Volume) * 1,
label = "Per. 4",
color = "black",
size = 2.5,
fontface = "bold") +
theme_minimal() +
labs(title = "Coverage volume by week, all outlets",
y = "Coverage (15-sec. clips)",,
x = "Week & periods")
Figure
# Means comparison
if (!require("gtExtras"))
install.packages("gtExtras")
library(gtExtras)
mydata <- AllData
# Specify the DV and IV
mydata$DV <- mydata$Ukraine
mydata$Period <- mydata$Period
# Calculate and show the group counts, means, standard
# deviations, minimums, maximums, and medians
Descriptives <- group_by(mydata, Period) %>%
summarise(
count = n(),
mean = mean(DV, na.rm = TRUE),
sd = sd(DV, na.rm = TRUE),
min = min(DV, na.rm = TRUE),
max = max(DV, na.rm = TRUE),
med = median(DV, na.rm = TRUE)
)
Desc_Table <- gt(Descriptives) %>%
tab_header("Descriptive statistics") %>%
cols_align(align = "left") %>%
gt_theme_538
Desc_Table
# Kruskal-Wallis test & Dunn's post hoc test
if (!require("FSA"))
install.packages("FSA")
library(FSA)
options(scipen=0)
kruskal.test(DV ~ Period, data = mydata)
dunnTest(DV ~ Period, data = mydata)
# ANOVA & Tukey post hoc test
options(scipen = 999)
oneway.test(mydata$DV ~ mydata$Period,
var.equal = FALSE)
anova_1 <- aov(mydata$DV ~ mydata$Period)
TukeyHSD(anova_1)
# Show ANOVA results in table form
# Load necessary libraries
library(gt)
library(gtExtras)
library(dplyr)
library(broom)
# Run ANOVA
anova_1 <- aov(DV ~ Period, data = mydata)
anova_results <- tidy(anova_1)
# Run Tukey HSD test
tukey_results <- TukeyHSD(anova_1)
tukey_df <- tidy(tukey_results)
# Format ANOVA table with gt
anova_table <- anova_results %>%
gt() %>%
tab_header(title = "ANOVA Results") %>%
fmt_number(columns = where(is.numeric), decimals = 3) %>%
gt_theme_538()
# Format Tukey HSD results with gt
tukey_table <- tukey_df %>%
select(contrast, estimate, conf.low, conf.high, adj.p.value) %>%
rename(Comparison = contrast, Diff = estimate, Lower = conf.low, Upper = conf.high, p_value = adj.p.value) %>%
gt() %>%
tab_header(title = "Tukey HSD Post-Hoc Test") %>%
fmt_number(columns = where(is.numeric), decimals = 3) %>%
data_color(columns = "p_value",
colors = scales::col_numeric(palette = c("darkgray", "lightgray", "white"), domain = c(0, 1))) %>%
gt_theme_538() # Adding a theme for styling
# Display tables
anova_table
tukey_table