---
title: "Visualization Guide"
author: "Zaoqu Liu"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Visualization Guide}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5,
  message = FALSE,
  warning = FALSE
)
```

## Introduction

This vignette demonstrates various ways to visualize SCENT results for 
publication-quality figures.

```{r load}
library(SCENT)
library(ggplot2)
library(viridis)

# Load data
data(net13Jun12.m)

# Simulate data with known structure
set.seed(2024)
n_genes <- 5500

# Create 3 cell populations with different potency levels
n_per_group <- 50

# High potency (stem-like): broad expression
exp_high <- matrix(rpois(n_genes * n_per_group, 5), nrow = n_genes)

# Medium potency: intermediate
exp_med <- matrix(rpois(n_genes * n_per_group, 3), nrow = n_genes)
exp_med[1:1000, ] <- rpois(1000 * n_per_group, 10)

# Low potency (differentiated): focused expression
exp_low <- matrix(rpois(n_genes * n_per_group, 2), nrow = n_genes)
exp_low[1:500, ] <- rpois(500 * n_per_group, 20)

# Combine
exp_all <- cbind(exp_high, exp_med, exp_low)
rownames(exp_all) <- head(rownames(net13Jun12.m), n_genes)
colnames(exp_all) <- paste0("Cell_", 1:ncol(exp_all))

# Cell annotations
cell_groups <- factor(
  rep(c("High Potency", "Medium Potency", "Low Potency"), each = n_per_group),
  levels = c("High Potency", "Medium Potency", "Low Potency")
)

# Compute scores
integ <- DoIntegPPI(exp_all, net13Jun12.m)
sr <- CompSRana(integ, local = TRUE)
ccat <- CompCCAT(exp_all, net13Jun12.m)

# Create data frame
df <- data.frame(
  Cell = colnames(exp_all),
  Group = cell_groups,
  SR = sr$SR,
  CCAT = ccat
)

cat("Data prepared:", nrow(df), "cells in", length(unique(df$Group)), "groups\n")
```

## 1. Distribution Plots

### Box Plot with Individual Points

```{r boxplot, fig.height=5}
ggplot(df, aes(x = Group, y = SR, fill = Group)) +
  geom_boxplot(alpha = 0.7, outlier.shape = NA, width = 0.6) +
  geom_jitter(width = 0.15, alpha = 0.4, size = 1.5) +
  scale_fill_viridis_d(option = "plasma", begin = 0.2, end = 0.8) +
  labs(
    title = "Signaling Entropy Rate by Cell Population",
    subtitle = "Higher SR indicates higher differentiation potency",
    x = "",
    y = "Signaling Entropy Rate (SR)"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5, color = "gray40"),
    legend.position = "none",
    panel.grid.minor = element_blank()
  )
```

### Violin Plot

```{r violin, fig.height=5}
ggplot(df, aes(x = Group, y = SR, fill = Group)) +
  geom_violin(alpha = 0.7, trim = FALSE) +
  geom_boxplot(width = 0.1, fill = "white", alpha = 0.8) +
  scale_fill_manual(values = c("#2ecc71", "#f39c12", "#e74c3c")) +
  labs(
    title = "Distribution of SR Scores",
    x = "",
    y = "Signaling Entropy Rate"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),
    legend.position = "none"
  )
```

## 2. Scatter Plots

### SR vs CCAT Correlation

```{r scatter, fig.height=5}
ggplot(df, aes(x = CCAT, y = SR, color = Group)) +
  geom_point(alpha = 0.7, size = 2.5) +
  geom_smooth(method = "lm", se = FALSE, linetype = "dashed", 
              aes(group = 1), color = "gray30") +
  scale_color_manual(values = c("#2ecc71", "#f39c12", "#e74c3c")) +
  labs(
    title = "SR vs CCAT by Cell Population",
    subtitle = paste("Overall correlation: r =", 
                     round(cor(df$SR, df$CCAT), 3)),
    x = "CCAT Score",
    y = "Signaling Entropy Rate"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5),
    legend.position = "right"
  )
```

## 3. Density Plots

### Overlapping Densities

```{r density, fig.height=4}
ggplot(df, aes(x = SR, fill = Group)) +
  geom_density(alpha = 0.5) +
  scale_fill_manual(values = c("#2ecc71", "#f39c12", "#e74c3c")) +
  labs(
    title = "SR Score Density by Population",
    x = "Signaling Entropy Rate",
    y = "Density"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),
    legend.position = "top"
  )
```

### Ridge Plot Style

```{r ridge, fig.height=4}
ggplot(df, aes(x = SR, y = Group, fill = Group)) +
  geom_violin(scale = "width", trim = FALSE) +
  scale_fill_manual(values = c("#2ecc71", "#f39c12", "#e74c3c")) +
  labs(
    title = "SR Distribution Comparison",
    x = "Signaling Entropy Rate",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),
    legend.position = "none"
  ) +
  coord_flip()
```

## 4. Statistical Comparison

```{r stats}
# Pairwise comparisons
groups <- levels(df$Group)
cat("Statistical Comparisons (Wilcoxon test):\n\n")

for (i in 1:(length(groups)-1)) {
  for (j in (i+1):length(groups)) {
    g1 <- df$SR[df$Group == groups[i]]
    g2 <- df$SR[df$Group == groups[j]]
    test <- wilcox.test(g1, g2)
    cat(sprintf("%s vs %s: p = %.2e\n", 
                groups[i], groups[j], test$p.value))
  }
}
```

### Significance Annotation

```{r signif-plot, fig.height=5}
# Manual significance brackets
max_sr <- max(df$SR)

ggplot(df, aes(x = Group, y = SR, fill = Group)) +
  geom_boxplot(alpha = 0.7, outlier.shape = NA) +
  geom_jitter(width = 0.15, alpha = 0.3, size = 1) +
  scale_fill_manual(values = c("#2ecc71", "#f39c12", "#e74c3c")) +
  # Add significance annotations
  annotate("segment", x = 1, xend = 2, y = max_sr + 0.005, yend = max_sr + 0.005) +
  annotate("text", x = 1.5, y = max_sr + 0.008, label = "***", size = 5) +
  annotate("segment", x = 2, xend = 3, y = max_sr + 0.015, yend = max_sr + 0.015) +
  annotate("text", x = 2.5, y = max_sr + 0.018, label = "***", size = 5) +
  annotate("segment", x = 1, xend = 3, y = max_sr + 0.025, yend = max_sr + 0.025) +
  annotate("text", x = 2, y = max_sr + 0.028, label = "***", size = 5) +
  labs(
    title = "SR Differences Between Populations",
    subtitle = "*** p < 0.001 (Wilcoxon test)",
    x = "",
    y = "Signaling Entropy Rate"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),
    plot.subtitle = element_text(hjust = 0.5, color = "gray40"),
    legend.position = "none"
  ) +
  ylim(NA, max_sr + 0.035)
```

## 5. Local Entropy Heatmap

```{r heatmap, fig.height=5, fig.width=8}
# Get top variable genes by local entropy variance
locS_var <- apply(sr$locS, 1, var)
top_idx <- order(locS_var, decreasing = TRUE)[1:30]

# Subset and scale
locS_top <- sr$locS[top_idx, ]
locS_scaled <- t(scale(t(locS_top)))

# Reorder columns by group
order_idx <- order(cell_groups)
locS_ordered <- locS_scaled[, order_idx]

# Use base R image for simplicity
image(
  t(locS_ordered),
  col = viridis::viridis(100),
  axes = FALSE,
  main = "Local Entropy Heatmap\n(Top 30 variable genes)",
  xlab = "Cells (ordered by potency)",
  ylab = "Genes"
)
```

## 6. Summary Statistics Table

```{r summary}
# Summary statistics using base R
summary_list <- lapply(levels(df$Group), function(g) {
  sub <- df[df$Group == g, ]
  data.frame(
    Group = g,
    N = nrow(sub),
    SR_Mean = round(mean(sub$SR), 4),
    SR_SD = round(sd(sub$SR), 4),
    CCAT_Mean = round(mean(sub$CCAT), 4),
    CCAT_SD = round(sd(sub$CCAT), 4)
  )
})
summary_df <- do.call(rbind, summary_list)

knitr::kable(
  summary_df,
  caption = "Summary Statistics by Cell Population"
)
```

## Publication Tips

1. **Color schemes**: Use colorblind-friendly palettes (viridis, ColorBrewer)
2. **Font sizes**: Ensure readability at final publication size
3. **Statistical annotations**: Always include p-values or significance levels
4. **Error bars**: Show SD or 95% CI for mean comparisons
5. **Sample sizes**: Report n for each group

## Session Info

```{r session}
sessionInfo()
```
