library(ggplot2)
library(dplyr)
library(tidyr)
library(gridExtra)
# -----------------------------------------------------------------------------
# 1. Prepare Wide Data & Compute 6x6 Correlation Matrix
# -----------------------------------------------------------------------------
# Create short labels (e.g., Basal_BST, Carb_VDB) and pivot to wide format
rat_brain_wide <- rat_brain_long %>%
mutate(
cond_label = paste(
ifelse(treatment == "Carbachol", "Carb", as.character(treatment)),
region,
sep = "_"
)
) %>%
select(animal, cond_label, activate) %>%
pivot_wider(names_from = cond_label, values_from = activate)
# Compute 6x6 correlation matrix across the 6 condition columns
cor_matrix <- cor(rat_brain_wide %>% select(-animal), use = "pairwise.complete.obs")
# Convert matrix to long format for ggplot heatmap visualization
cor_df <- as.data.frame(cor_matrix) %>%
mutate(Var1 = rownames(.)) %>%
pivot_longer(-Var1, names_to = "Var2", values_to = "Correlation")
# -----------------------------------------------------------------------------
# 2. Panel A: 6x6 Correlation Heatmap
# -----------------------------------------------------------------------------
panel_a_cor <- ggplot(cor_df, aes(x = Var1, y = Var2, fill = Correlation)) +
geom_tile(color = "white", linewidth = 0.5) +
geom_text(aes(label = sprintf("%.2f", Correlation)), color = "black", size = 3.5) +
scale_fill_gradient2(low = "#4575b4", mid = "#ffffbf", high = "#d73027", midpoint = 0.5, limits = c(-1, 1)) +
labs(
title = "Empirical 6×6 Correlation Matrix",
x = "", y = "",
fill = "r"
) +
theme_minimal(base_size = 11) +
theme(
plot.title = element_text(face = "bold", size = 12),
axis.text.x = element_text(angle = 45, hjust = 1, face = "bold"),
axis.text.y = element_text(face = "bold"),
panel.grid = element_blank()
)
# -----------------------------------------------------------------------------
# 3. Panel B: Subject Profile Tracking Plot Across All 6 Conditions
# -----------------------------------------------------------------------------
# Define explicit ordering for the 6 condition combinations
condition_order <- c("Basal_VDB", "Basal_LS", "Basal_BST", "Carb_VDB", "Carb_LS", "Carb_BST")
tracking_data <- rat_brain_long %>%
mutate(
cond_label = factor(
paste(ifelse(treatment == "Carbachol", "Carb", as.character(treatment)), region, sep = "_"),
levels = condition_order
)
)
panel_b_tracking <- ggplot(tracking_data, aes(x = cond_label, y = activate, group = animal, color = animal)) +
geom_line(linewidth = 1, alpha = 0.85) +
geom_point(size = 2.5) +
labs(
title = "Subject Profile Tracking Across 6 Conditions",
subtitle = "Demonstrating Subject Rank-Order Consistency",
x = "Condition (Treatment_Region)",
y = "Nucleotide Activation",
color = "Rat ID"
) +
theme_minimal(base_size = 11) +
theme(
plot.title = element_text(face = "bold", size = 12),
plot.subtitle = element_text(color = "grey30", size = 9.5),
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "right",
panel.grid.minor = element_blank()
)
# -----------------------------------------------------------------------------
# Combine Plots Side-by-Side
# -----------------------------------------------------------------------------
grid.arrange(panel_a_cor, panel_b_tracking, ncol = 2, widths = c(1.1, 1.3))