Newly Created Plots with Same Conclusions

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.2     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.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
df <- read.csv("symptom_data.csv")

colnames(df)
##  [1] "UniqueID"         "timestamp_mjs"    "mjs_nose_run"     "mjs_nose_stuf"   
##  [5] "mjs_sneeze"       "mjs_throat_sr"    "mjs_earache"      "mjs_malaise"     
##  [9] "mjs_headache"     "mjs_mjache"       "mjs_chills"       "mjs_feverish"    
## [13] "mjs_chest_tight"  "mjs_breath_short" "mjs_cough"        "mjs_calc"        
## [17] "group"            "date"             "time_in_study"
df_symptoms <- df %>%
  select(UniqueID, starts_with("mjs_")) %>%
  select(-mjs_calc)   # remove non-symptom column

df_max <- df_symptoms %>%
  group_by(UniqueID) %>%
  summarise(across(everything(), max, na.rm = TRUE))
## Warning: There was 1 warning in `summarise()`.
## ℹ In argument: `across(everything(), max, na.rm = TRUE)`.
## ℹ In group 1: `UniqueID = "D24b-1"`.
## Caused by warning:
## ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
## Supply arguments directly to `.fns` through an anonymous function instead.
## 
##   # Previously
##   across(a:b, mean, na.rm = TRUE)
## 
##   # Now
##   across(a:b, \(x) mean(x, na.rm = TRUE))
df_long <- df_max %>%
  pivot_longer(-UniqueID, names_to = "Symptom", values_to = "Score")

df_long <- df_long %>%
  mutate(
    Symptom = recode(Symptom,
      "mjs_nose_run" = "Running nose",
      "mjs_nose_stuf" = "Stuffy nose",
      "mjs_sneeze" = "Sneeze",
      "mjs_throat_sr" = "Sore throat",
      "mjs_earache" = "Earache",
      "mjs_chest_tight" = "Chest tightness",
      "mjs_breath_short" = "Shortness of breath",
      "mjs_cough" = "Cough",
      "mjs_malaise" = "Malaise",
      "mjs_headache" = "Headache",
      "mjs_mjache" = "Muscle ache",
      "mjs_chills" = "Chills",
      "mjs_feverish" = "Feverish"
    ),
    
    Group = case_when(
      Symptom %in% c("Running nose","Stuffy nose","Sneeze","Sore throat","Earache") ~ "Upper respiratory",
      Symptom %in% c("Chest tightness","Shortness of breath","Cough") ~ "Lower respiratory",
      TRUE ~ "Systemic"
    )
  )

df_long$Symptom <- factor(df_long$Symptom, levels = c(
  "Running nose","Stuffy nose","Sneeze","Sore throat","Earache",
  "Chest tightness","Shortness of breath","Cough",
  "Malaise","Headache","Muscle ache","Chills","Feverish"
))


p3 = ggplot(df_long, aes(x = Symptom, y = UniqueID, fill = Score)) +
  geom_tile(color = "white") +
  geom_text(aes(label = Score), size = 3) +
  
  scale_fill_gradient(
    low = "#f0f0f0",
    high = "#b30000",
    limits = c(0,3),
    name = "Severity"
  ) +
  
  facet_grid(~ Group, scales = "free_x", space = "free_x") +
  theme_minimal() +
    theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    panel.grid = element_blank(),
    strip.text = element_text(face = "bold")
  ) +
  labs(
    title = "Symptom Severity Across Participants",
    subtitle = "Maximum reported severity (0–3)",
    x = NULL,
    y = "Participant"
  )


ggsave("plot.png", plot = p3, width = 8, height = 6)


p3

df_summary <- df_long %>%
  group_by(Group, Symptom) %>%
  summarise(mean_score = mean(Score), .groups = "drop")

ggplot(df_summary, aes(x = Symptom, y = mean_score, fill = Group)) +
  geom_col() +
  facet_wrap(~ Group, scales = "free_x") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(
    title = "Average Symptom Severity by Group",
    y = "Mean Severity (0–3)",
    x = NULL
  )

df_long <- df_long %>%
  mutate(
    ID_group = ifelse(startsWith(UniqueID, "D"), "D", "R")
  )

df_summary <- df_long %>%
  group_by(ID_group, Group, Symptom) %>%
  summarise(mean_score = mean(Score), .groups = "drop")


p4 = ggplot(df_summary, aes(x = Symptom, y = mean_score, fill = ID_group)) +
  geom_col(position = "dodge") +
  
  facet_wrap(~ Group, scales = "free_x") +
  
  scale_fill_manual(values = c("D" = "salmon", "R" = "turquoise"),
                    labels = c("D" = "Donor", "R" = "Recipient")) +
  
  theme_test() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    strip.text = element_text(face = "bold")
  ) +
  scale_y_continuous(
    breaks = seq(0, 3, 0.5),
    limits = c(0, 3))+ 
  labs(
    title = "Average Symptom Severity by Participant Group",
    x = NULL,
    y = "Mean Severity (0–3)",
    fill = "Participant Type"
  ) 

ggsave("new.png", plot = p4, width = 8, height = 4)