Load the necessary libraries

library(ggplot2)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(readxl)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)

Read and Prepare Data

media_data <- read_excel("~/Desktop/Desktop - Med’s MacBook Pro/Data/MediaSearchesTunisiaWarGaza.xlsx")
media_data$Day <- as.Date(media_data$Day, format="%Y-%m-%d")
# Assuming 'media_data' is a data frame with a 'Day' column and other columns for each media outlet

Plot Search Interest Over Time

ggplot(media_data, aes(x = Day)) + 
  geom_line(aes(y = AlJazeera, linetype = "Al Jazeera"), color = "black", size = 1) +
  geom_line(aes(y = MosaiqueFM, linetype = "Mosaïque FM"), color = "#404040", size = 1) +
  geom_line(aes(y = ShemsFM, linetype = "Shems FM"), color = "#606060", size = 1) +
  geom_line(aes(y = DiwanFM, linetype = "Diwan FM"), color = "#808080", size = 1) +
  geom_line(aes(y = AlArabiya, linetype = "Al Arabiya"), color = "#A0A0A0", size = 1) +
  geom_vline(xintercept = as.numeric(as.Date("2023-10-07")), color = "red", linetype = "dashed", size = 1.5) +
  scale_linetype_manual(name = "Media Outlet", 
                        values = c("dotted", "twodash", "solid", "dotdash", "longdash")) +
  labs(x = "Date", y = "Search Interest (Relative to Peak Popularity)", 
       title = "Media Outlet Search Interest Before and After October 7, 2023", 
       caption = 'Mohamed Dhia Hammami',
       subtitle = "The intervention point (start of the war) is marked with a red line.") +
  theme_minimal() +
  theme(legend.position = "bottom", 
        axis.text.x = element_text(angle = 45, hjust = 1))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Classify Data and Plot Comparisons

# Assuming the war started on October 7th and hasn't ended yet
start_war <- as.Date("2023-10-07")

# Add a new variable to classify the data into 'Pre-War' and 'Wartime'
media_data <- media_data %>%
  mutate(Period = ifelse(Day < start_war, "Pre-War", "Wartime"))

# If your data contains a column for each media outlet with its search interest, the previous group_by is unnecessary.
# Instead, for plotting purposes, you need to convert your data to long format:
media_data_long <- media_data %>%
  pivot_longer(cols = AlJazeera:AlArabiya, names_to = "Media_Outlet", values_to = "Search_Interest")

# Now you can create a summarized data frame for the plot
media_summary <- media_data_long %>%
  group_by(Media_Outlet, Period) %>%
  summarize(Mean_Interest = mean(Search_Interest, na.rm = TRUE), .groups = 'drop')

# Plotting the bar plot
ggplot(media_summary, aes(x = Media_Outlet, y = Mean_Interest, fill = Period)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = c('Pre-War' = 'darkgrey', 'Wartime' = 'black')) +
  labs(title = 'Pre-war vs. Wartime Average Google Search Interest for Each Media Outlet',
       subtitle = 'Tunisia during the 2023 War on Gaza',
       caption = 'Mohamed Dhia Hammami',
       x = 'Media Outlet',
       y = 'Average Search Interest (Relative to Peak Popularity)') +
  theme_minimal() +
  theme(legend.title = element_blank(), axis.text.x = element_text(angle = 45, hjust = 1))