g2 <- comparisons[[i]][2]
x1 <- subdat %>% filter(Cell.type == g1) %>% pull(NucPolk_perArea)
x2 <- subdat %>% filter(Cell.type == g2) %>% pull(NucPolk_perArea)
wt <- wilcox.test(x1, x2, exact = FALSE)
d_val <- effsize::cohen.d(x1, x2, hedges.correction = FALSE)$estimate
y_here <- base + (length(comparisons) - i) * step
ann_list[[k]] <- data.frame(
age_panel = age_i,
area = factor(area_i, levels = c("M1", "S1")),
xstart = area_x[[area_i]] + cell_offset[[g1]],
xend   = area_x[[area_i]] + cell_offset[[g2]],
xmid   = area_x[[area_i]] + mean(c(cell_offset[[g1]], cell_offset[[g2]])),
y      = y_here,
ytip   = y_here - tip_drop,
ytext  = y_here + text_nudge,
label  = paste0(fmt_p(wt$p.value), "\nCohen's d=", sprintf("%.2f", abs(unname(d_val))))
)
k <- k + 1
}
}
}
ann_df <- do.call(rbind, ann_list)
ann_df$age_panel <- factor(ann_df$age_panel, levels = c("Young", "Old (19M - 24M)"))
# Generate graph
graph <- ggplot(flt.data, aes(x = area, y = NucPolk_perArea, fill = Cell.type)) +
geom_boxplot(
notch = FALSE,
linewidth = 0.25,
alpha = 0.30,
position = position_dodge(width = 0.8),
outlier.size = 1.2
) +
facet_grid(cols = vars(age_panel), scales = "fixed", space = "fixed") +
geom_segment(
data = ann_df,
aes(x = xstart, xend = xend, y = y, yend = y),
inherit.aes = FALSE,
linewidth = 0.3
) +
geom_segment(
data = ann_df,
aes(x = xstart, xend = xstart, y = y, yend = ytip),
inherit.aes = FALSE,
linewidth = 0.3
) +
geom_segment(
data = ann_df,
aes(x = xend, xend = xend, y = y, yend = ytip),
inherit.aes = FALSE,
linewidth = 0.3
) +
geom_text(
data = ann_df,
aes(x = xmid, y = ytext, label = label),
inherit.aes = FALSE,
size = 2,
lineheight = 0.95
) +
labs(x = "brain area", y = "nuclear POLK counts per area") +
theme_classic() +
theme(
axis.title = element_text(size = 10),
axis.text = element_text(size = 10),
strip.text = element_text(size = 12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "none",
plot.margin = margin(12, 30, 12, 12)
) +
coord_cartesian(clip = "off")
print(graph)
# Optional save lines
# ggsave("Fig4_with_p_and_cohens_d_young_left.png", graph, width = 8.5, height = 5.5, dpi = 300)
# ggsave("Fig4_with_p_and_cohens_d_young_left.pdf", graph, width = 8.5, height = 5.5)
library(dplyr)
library(ggplot2)
library(effsize)
# Set working directory
setwd("/Users/anirban/MyLab/Manuscript/Mofida_Polk-aging/Mar26_VOR/Plot_source.data/Figure4/")
# Read .csv into a dataframe
# read.csv() converts "Cell-type" -> "Cell.type" by default
data <- read.csv("./combo_INPNNN_TestV4_20221212_IN.PN.NN_Obj.csv", header = TRUE, sep = ",")
# Rearrange factor levels
data$area <- factor(data$area, levels = c("Cg1", "M2", "M1", "S1", "RSD"))
data$Cell.type <- factor(data$Cell.type, levels = c("IN", "PN", "NN"))
# IMPORTANT:
# The raw CSV uses age_bracket values YNG and OLD.
# Create a separate plotting facet variable so ordering is forced explicitly.
data$age_panel <- factor(
data$age_bracket,
levels = c("YNG", "OLD"),
labels = c("Young", "Old (19M - 24M)")
)
# IMPORTANT: use %in%, not == c("M1", "S1")
flt.data <- data %>%
filter(area %in% c("M1", "S1"))
# Re-assert factor order after filtering
flt.data$area <- factor(flt.data$area, levels = c("M1", "S1"))
flt.data$Cell.type <- factor(flt.data$Cell.type, levels = c("IN", "PN", "NN"))
flt.data$age_panel <- factor(
flt.data$age_panel,
levels = c("Young", "Old (19M - 24M)")
)
# Pairwise comparisons within each area and age panel
comparisons <- list(
c("IN", "NN"),
c("IN", "PN"),
c("PN", "NN")
)
# Exact p-value formatter
fmt_p <- function(p) {
if (is.na(p)) return(NA_character_)
if (p < 1e-4) {
paste0("p=", format(p, scientific = TRUE, digits = 2))
} else {
paste0("p=", sprintf("%.4f", p))
}
}
# Uncomment this instead if you want figure-style labels
fmt_p <- function(p) if (p < 0.001) "p<0.001" else paste0("p=", sprintf("%.3f", p))
# Manual x-positions for the three dodged boxes within each area
area_x <- c(M1 = 1, S1 = 2)
cell_offset <- c(IN = -0.26, PN = 0.00, NN = 0.26)
# Build annotation table with Mann-Whitney p-values + Cohen's d
ann_list <- list()
k <- 1
for (age_i in levels(flt.data$age_panel)) {
for (area_i in c("M1", "S1")) {
subdat <- flt.data %>% filter(age_panel == age_i, area == area_i)
y_max <- max(subdat$NucPolk_perArea, na.rm = TRUE)
y_rng <- diff(range(subdat$NucPolk_perArea, na.rm = TRUE))
if (y_rng == 0) y_rng <- y_max
step <- 0.13 * y_rng
base <- y_max + -0.35 * y_rng
text_nudge <- 0.025 * y_rng
tip_drop <- 0.00 * y_rng
for (i in seq_along(comparisons)) {
g1 <- comparisons[[i]][1]
g2 <- comparisons[[i]][2]
x1 <- subdat %>% filter(Cell.type == g1) %>% pull(NucPolk_perArea)
x2 <- subdat %>% filter(Cell.type == g2) %>% pull(NucPolk_perArea)
wt <- wilcox.test(x1, x2, exact = FALSE)
d_val <- effsize::cohen.d(x1, x2, hedges.correction = FALSE)$estimate
y_here <- base + (length(comparisons) - i) * step
ann_list[[k]] <- data.frame(
age_panel = age_i,
area = factor(area_i, levels = c("M1", "S1")),
xstart = area_x[[area_i]] + cell_offset[[g1]],
xend   = area_x[[area_i]] + cell_offset[[g2]],
xmid   = area_x[[area_i]] + mean(c(cell_offset[[g1]], cell_offset[[g2]])),
y      = y_here,
ytip   = y_here - tip_drop,
ytext  = y_here + text_nudge,
label  = paste0(fmt_p(wt$p.value), "\nCohen's d=", sprintf("%.2f", abs(unname(d_val))))
)
k <- k + 1
}
}
}
ann_df <- do.call(rbind, ann_list)
ann_df$age_panel <- factor(ann_df$age_panel, levels = c("Young", "Old (19M - 24M)"))
# Generate graph
graph <- ggplot(flt.data, aes(x = area, y = NucPolk_perArea, fill = Cell.type)) +
geom_boxplot(
notch = FALSE,
linewidth = 0.25,
alpha = 0.30,
position = position_dodge(width = 0.8),
outlier.size = 1.2
) +
facet_grid(cols = vars(age_panel), scales = "fixed", space = "fixed") +
geom_segment(
data = ann_df,
aes(x = xstart, xend = xend, y = y, yend = y),
inherit.aes = FALSE,
linewidth = 0.3
) +
geom_segment(
data = ann_df,
aes(x = xstart, xend = xstart, y = y, yend = ytip),
inherit.aes = FALSE,
linewidth = 0.3
) +
geom_segment(
data = ann_df,
aes(x = xend, xend = xend, y = y, yend = ytip),
inherit.aes = FALSE,
linewidth = 0.3
) +
geom_text(
data = ann_df,
aes(x = xmid, y = ytext, label = label),
inherit.aes = FALSE,
size = 2,
lineheight = 0.95
) +
labs(x = "brain area", y = "nuclear POLK counts per area") +
theme_classic() +
theme(
axis.title = element_text(size = 10),
axis.text = element_text(size = 10),
strip.text = element_text(size = 12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "none",
plot.margin = margin(12, 30, 12, 12)
) +
coord_cartesian(clip = "off")
print(graph)
# Optional save lines
# ggsave("Fig4_with_p_and_cohens_d_young_left.png", graph, width = 8.5, height = 5.5, dpi = 300)
# ggsave("Fig4_with_p_and_cohens_d_young_left.pdf", graph, width = 8.5, height = 5.5)
library(dplyr)
library(ggplot2)
library(effsize)
# Set working directory
setwd("/Users/anirban/MyLab/Manuscript/Mofida_Polk-aging/Mar26_VOR/Plot_source.data/Figure4/")
# Read .csv into a dataframe
# read.csv() converts "Cell-type" -> "Cell.type" by default
data <- read.csv("./Fig4_data_relColOnly.csv", header = TRUE, sep = ",")
# Rearrange factor levels
data$area <- factor(data$area, levels = c("M1", "S1"))
data$Cell.type <- factor(data$Cell.type, levels = c("IN", "PN", "NN"))
# IMPORTANT:
# The raw CSV uses age_bracket values YNG and OLD.
# Create a separate plotting facet variable so ordering is forced explicitly.
data$age_panel <- factor(
data$age_bracket,
levels = c("YNG", "OLD"),
labels = c("Young", "Old (19M - 24M)")
)
# Re-assert factor order after filtering
flt.data$Cell.type <- factor(flt.data$Cell.type, levels = c("IN", "PN", "NN"))
library(dplyr)
library(ggplot2)
library(effsize)
# Set working directory
setwd("/Users/anirban/MyLab/Manuscript/Mofida_Polk-aging/Mar26_VOR/Plot_source.data/Figure4/")
# Read .csv into a dataframe
# read.csv() converts "Cell-type" -> "Cell.type" by default
data <- read.csv("./Fig4_data_relColOnly.csv", header = TRUE, sep = ",")
# Rearrange factor levels
data$area <- factor(data$area, levels = c("M1", "S1"))
data$Cell.type <- factor(data$Cell.type, levels = c("IN", "PN", "NN"))
# IMPORTANT:
# The raw CSV uses age_bracket values YNG and OLD.
# Create a separate plotting facet variable so ordering is forced explicitly.
data$age_panel <- factor(
data$age_bracket,
levels = c("YNG", "OLD"),
labels = c("Young", "Old (19M - 24M)")
)
# Re-assert factor order after filtering
data$Cell.type <- factor(data$Cell.type, levels = c("IN", "PN", "NN"))
data$age_panel <- factor(
data$age_panel,
levels = c("Young", "Old (19M - 24M)")
)
# Pairwise comparisons within each area and age panel
comparisons <- list(
c("IN", "NN"),
c("IN", "PN"),
c("PN", "NN")
)
# Exact p-value formatter
fmt_p <- function(p) {
if (is.na(p)) return(NA_character_)
if (p < 1e-4) {
paste0("p=", format(p, scientific = TRUE, digits = 2))
} else {
paste0("p=", sprintf("%.4f", p))
}
}
# Uncomment this instead if you want figure-style labels
fmt_p <- function(p) if (p < 0.001) "p<0.001" else paste0("p=", sprintf("%.3f", p))
# Manual x-positions for the three dodged boxes within each area
area_x <- c(M1 = 1, S1 = 2)
cell_offset <- c(IN = -0.26, PN = 0.00, NN = 0.26)
# Build annotation table with Mann-Whitney p-values + Cohen's d
ann_list <- list()
k <- 1
for (age_i in levels(data$age_panel)) {
for (area_i in c("M1", "S1")) {
subdat <- data %>% filter(age_panel == age_i, area == area_i)
y_max <- max(subdat$NucPolk_perArea, na.rm = TRUE)
y_rng <- diff(range(subdat$NucPolk_perArea, na.rm = TRUE))
if (y_rng == 0) y_rng <- y_max
step <- 0.13 * y_rng
base <- y_max + -0.35 * y_rng
text_nudge <- 0.025 * y_rng
tip_drop <- 0.00 * y_rng
for (i in seq_along(comparisons)) {
g1 <- comparisons[[i]][1]
g2 <- comparisons[[i]][2]
x1 <- subdat %>% filter(Cell.type == g1) %>% pull(NucPolk_perArea)
x2 <- subdat %>% filter(Cell.type == g2) %>% pull(NucPolk_perArea)
wt <- wilcox.test(x1, x2, exact = FALSE)
d_val <- effsize::cohen.d(x1, x2, hedges.correction = FALSE)$estimate
y_here <- base + (length(comparisons) - i) * step
ann_list[[k]] <- data.frame(
age_panel = age_i,
area = factor(area_i, levels = c("M1", "S1")),
xstart = area_x[[area_i]] + cell_offset[[g1]],
xend   = area_x[[area_i]] + cell_offset[[g2]],
xmid   = area_x[[area_i]] + mean(c(cell_offset[[g1]], cell_offset[[g2]])),
y      = y_here,
ytip   = y_here - tip_drop,
ytext  = y_here + text_nudge,
label  = paste0(fmt_p(wt$p.value), "\nCohen's d=", sprintf("%.2f", abs(unname(d_val))))
)
k <- k + 1
}
}
}
ann_df <- do.call(rbind, ann_list)
ann_df$age_panel <- factor(ann_df$age_panel, levels = c("Young", "Old (19M - 24M)"))
# Generate graph
graph <- ggplot(data, aes(x = area, y = NucPolk_perArea, fill = Cell.type)) +
geom_boxplot(
notch = FALSE,
linewidth = 0.25,
alpha = 0.30,
position = position_dodge(width = 0.8),
outlier.size = 1.2
) +
facet_grid(cols = vars(age_panel), scales = "fixed", space = "fixed") +
geom_segment(
data = ann_df,
aes(x = xstart, xend = xend, y = y, yend = y),
inherit.aes = FALSE,
linewidth = 0.3
) +
geom_segment(
data = ann_df,
aes(x = xstart, xend = xstart, y = y, yend = ytip),
inherit.aes = FALSE,
linewidth = 0.3
) +
geom_segment(
data = ann_df,
aes(x = xend, xend = xend, y = y, yend = ytip),
inherit.aes = FALSE,
linewidth = 0.3
) +
geom_text(
data = ann_df,
aes(x = xmid, y = ytext, label = label),
inherit.aes = FALSE,
size = 2,
lineheight = 0.95
) +
labs(x = "brain area", y = "nuclear POLK counts per area") +
theme_classic() +
theme(
axis.title = element_text(size = 10),
axis.text = element_text(size = 10),
strip.text = element_text(size = 12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "none",
plot.margin = margin(12, 30, 12, 12)
) +
coord_cartesian(clip = "off")
print(graph)
# Optional save lines
# ggsave("Fig4_with_p_and_cohens_d_young_left.png", graph, width = 8.5, height = 5.5, dpi = 300)
# ggsave("Fig4_with_p_and_cohens_d_young_left.pdf", graph, width = 8.5, height = 5.5)
library(dplyr)
library(ggplot2)
library(effsize)
# Set working directory
setwd("/Users/anirban/MyLab/Manuscript/Mofida_Polk-aging/Mar26_VOR/Plot_source.data/Figure4/")
# Read .csv into a dataframe
# read.csv() converts "Cell-type" -> "Cell.type" by default
data <- read.csv("./Fig4_data_relColOnly.csv", header = TRUE, sep = ",")
# Read .csv into a dataframe
# read.csv() converts "Cell-type" -> "Cell.type" by default
data <- read.csv("./Fig4_data.csv", header = TRUE, sep = ",")
library(dplyr)
library(ggplot2)
library(effsize)
# Set working directory
setwd("/Users/anirban/MyLab/Manuscript/Mofida_Polk-aging/Mar26_VOR/Plot_source.data/Figure4/")
# Read .csv into a dataframe
# read.csv() converts "Cell-type" -> "Cell.type" by default
data <- read.csv("./Fig4_data.csv", header = TRUE, sep = ",")
# Rearrange factor levels
data$area <- factor(data$area, levels = c("M1", "S1"))
data$Cell.type <- factor(data$Cell.type, levels = c("IN", "PN", "NN"))
# IMPORTANT:
# The raw CSV uses age_bracket values YNG and OLD.
# Create a separate plotting facet variable so ordering is forced explicitly.
data$age_panel <- factor(
data$age_bracket,
levels = c("YNG", "OLD"),
labels = c("Young", "Old (19M - 24M)")
)
# Re-assert factor order after filtering
data$Cell.type <- factor(data$Cell.type, levels = c("IN", "PN", "NN"))
data$age_panel <- factor(
data$age_panel,
levels = c("Young", "Old (19M - 24M)")
)
# Pairwise comparisons within each area and age panel
comparisons <- list(
c("IN", "NN"),
c("IN", "PN"),
c("PN", "NN")
)
# Exact p-value formatter
fmt_p <- function(p) {
if (is.na(p)) return(NA_character_)
if (p < 1e-4) {
paste0("p=", format(p, scientific = TRUE, digits = 2))
} else {
paste0("p=", sprintf("%.4f", p))
}
}
# Uncomment this instead if you want figure-style labels
fmt_p <- function(p) if (p < 0.001) "p<0.001" else paste0("p=", sprintf("%.3f", p))
# Manual x-positions for the three dodged boxes within each area
area_x <- c(M1 = 1, S1 = 2)
cell_offset <- c(IN = -0.26, PN = 0.00, NN = 0.26)
# Build annotation table with Mann-Whitney p-values + Cohen's d
ann_list <- list()
k <- 1
for (age_i in levels(data$age_panel)) {
for (area_i in c("M1", "S1")) {
subdat <- data %>% filter(age_panel == age_i, area == area_i)
y_max <- max(subdat$NucPolk_perArea, na.rm = TRUE)
y_rng <- diff(range(subdat$NucPolk_perArea, na.rm = TRUE))
if (y_rng == 0) y_rng <- y_max
step <- 0.13 * y_rng
base <- y_max + -0.35 * y_rng
text_nudge <- 0.025 * y_rng
tip_drop <- 0.00 * y_rng
for (i in seq_along(comparisons)) {
g1 <- comparisons[[i]][1]
g2 <- comparisons[[i]][2]
x1 <- subdat %>% filter(Cell.type == g1) %>% pull(NucPolk_perArea)
x2 <- subdat %>% filter(Cell.type == g2) %>% pull(NucPolk_perArea)
wt <- wilcox.test(x1, x2, exact = FALSE)
d_val <- effsize::cohen.d(x1, x2, hedges.correction = FALSE)$estimate
y_here <- base + (length(comparisons) - i) * step
ann_list[[k]] <- data.frame(
age_panel = age_i,
area = factor(area_i, levels = c("M1", "S1")),
xstart = area_x[[area_i]] + cell_offset[[g1]],
xend   = area_x[[area_i]] + cell_offset[[g2]],
xmid   = area_x[[area_i]] + mean(c(cell_offset[[g1]], cell_offset[[g2]])),
y      = y_here,
ytip   = y_here - tip_drop,
ytext  = y_here + text_nudge,
label  = paste0(fmt_p(wt$p.value), "\nCohen's d=", sprintf("%.2f", abs(unname(d_val))))
)
k <- k + 1
}
}
}
ann_df <- do.call(rbind, ann_list)
ann_df$age_panel <- factor(ann_df$age_panel, levels = c("Young", "Old (19M - 24M)"))
# Generate graph
graph <- ggplot(data, aes(x = area, y = NucPolk_perArea, fill = Cell.type)) +
geom_boxplot(
notch = FALSE,
linewidth = 0.25,
alpha = 0.30,
position = position_dodge(width = 0.8),
outlier.size = 1.2
) +
facet_grid(cols = vars(age_panel), scales = "fixed", space = "fixed") +
geom_segment(
data = ann_df,
aes(x = xstart, xend = xend, y = y, yend = y),
inherit.aes = FALSE,
linewidth = 0.3
) +
geom_segment(
data = ann_df,
aes(x = xstart, xend = xstart, y = y, yend = ytip),
inherit.aes = FALSE,
linewidth = 0.3
) +
geom_segment(
data = ann_df,
aes(x = xend, xend = xend, y = y, yend = ytip),
inherit.aes = FALSE,
linewidth = 0.3
) +
geom_text(
data = ann_df,
aes(x = xmid, y = ytext, label = label),
inherit.aes = FALSE,
size = 2,
lineheight = 0.95
) +
labs(x = "brain area", y = "nuclear POLK counts per area") +
theme_classic() +
theme(
axis.title = element_text(size = 10),
axis.text = element_text(size = 10),
strip.text = element_text(size = 12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "none",
plot.margin = margin(12, 30, 12, 12)
) +
coord_cartesian(clip = "off")
print(graph)
# Optional save lines
# ggsave("Fig4_with_p_and_cohens_d_young_left.png", graph, width = 8.5, height = 5.5, dpi = 300)
# ggsave("Fig4_with_p_and_cohens_d_young_left.pdf", graph, width = 8.5, height = 5.5)
