--- title: "Visualization Gallery" author: "Zaoqu Liu" date: "`r Sys.Date()`" output: rmarkdown::html_vignette: toc: true toc_depth: 2 vignette: > %\VignetteIndexEntry{Visualization Gallery} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 8, fig.height = 6, message = FALSE, warning = FALSE ) ``` ## Overview SecAct provides a comprehensive suite of visualization functions for exploring secreted protein activity results. This gallery showcases all available plotting functions with customization options. ```{r load} library(SecAct) library(ggplot2) # Load and process data data_path <- system.file("extdata/GSE100093.IFNG.expr.gz", package = "SecAct") expr <- read.table(gzfile(data_path), sep = "\t", header = TRUE, row.names = 1) # Run activity inference result <- SecAct.activity.inference(expr[, 1:8], lambda = 5e5, nrand = 100) ``` ## Activity Heatmaps ### Basic Heatmap ```{r heatmap-basic, fig.cap="Basic activity heatmap"} # Select top 20 most variable secreted proteins var_sp <- apply(result$zscore, 1, var) top20 <- names(sort(var_sp, decreasing = TRUE))[1:20] SecAct.heatmap.plot(result$zscore[top20, ]) ``` ### Customized Colors ```{r heatmap-colors, fig.cap="Heatmap with custom color palette"} # Use different color scheme SecAct.heatmap.plot( result$zscore[top20, ], title = "Secreted Protein Activity", colors = c("#2166AC", "#F7F7F7", "#B2182B") # Blue-white-red ) ``` ### Divergent Scale ```{r heatmap-divergent, fig.cap="Heatmap with divergent scale"} # Center around zero for better visualization SecAct.heatmap.plot( result$zscore[top20, ], title = "Activity Z-scores", colors = c("#4575B4", "#91BFDB", "#E0F3F8", "#FFFFBF", "#FEE090", "#FC8D59", "#D73027") ) ``` ## Bar Plots ### Basic Bar Plot ```{r bar-basic, fig.cap="Basic bar plot"} # Get top up/down regulated secreted proteins activities <- result$zscore[, 1] n <- 10 top_up <- names(sort(activities, decreasing = TRUE))[1:n] top_down <- names(sort(activities))[1:n] selected <- c(top_up, top_down) SecAct.bar.plot(activities[selected]) ``` ### With Title ```{r bar-titled, fig.cap="Bar plot with custom title"} SecAct.bar.plot( activities[selected], title = "Sample 1: Top Active Secreted Proteins" ) ``` ### Custom Colors ```{r bar-colors, fig.cap="Bar plot with custom colors"} SecAct.bar.plot( activities[selected], title = "Activity Change", colors = c("#3288BD", "#D53E4F") # Blue for down, red for up ) ``` ## Lollipop Plots ### Basic Lollipop ```{r lollipop-basic, fig.cap="Basic lollipop plot"} SecAct.lollipop.plot(activities[selected]) ``` ### With Title ```{r lollipop-titled, fig.cap="Lollipop plot with title"} SecAct.lollipop.plot( activities[selected], title = "Secreted Protein Activity Rankings" ) ``` ## Combining Plots ### Side-by-Side Comparison ```{r comparison, fig.width=12, fig.height=5, fig.cap="Comparing multiple samples"} # Compare two samples sample1_act <- result$zscore[selected, 1] sample2_act <- result$zscore[selected, 2] p1 <- SecAct.bar.plot(sample1_act, title = "Sample 1") p2 <- SecAct.bar.plot(sample2_act, title = "Sample 2") # Use patchwork to combine if(requireNamespace("patchwork", quietly = TRUE)) { patchwork::wrap_plots(p1, p2, ncol = 2) } ``` ## Publication-Ready Figures ### Customized Theme ```{r publication, fig.cap="Publication-quality visualization"} # Create a polished visualization activities_df <- data.frame( protein = factor(names(activities[selected]), levels = names(sort(activities[selected]))), zscore = activities[selected], direction = ifelse(activities[selected] > 0, "Upregulated", "Downregulated") ) ggplot(activities_df, aes(x = protein, y = zscore, fill = direction)) + geom_bar(stat = "identity", width = 0.8) + coord_flip() + scale_fill_manual(values = c("Downregulated" = "#4575B4", "Upregulated" = "#D73027")) + labs( title = "Secreted Protein Activity Profile", subtitle = "Sample 1 vs Background", x = NULL, y = "Activity Z-score", fill = "Direction" ) + theme_minimal(base_size = 12) + theme( plot.title = element_text(face = "bold", hjust = 0.5), plot.subtitle = element_text(hjust = 0.5, color = "gray50"), panel.grid.major.y = element_blank(), panel.grid.minor = element_blank(), legend.position = "bottom" ) ``` ## Statistical Visualization ### P-value Distribution ```{r pvalue-dist, fig.cap="P-value distribution"} pvals <- as.vector(result$pvalue) par(mfrow = c(1, 2)) # Histogram hist(pvals, breaks = 50, col = "steelblue", border = "white", main = "P-value Distribution", xlab = "P-value", xlim = c(0, 1)) abline(v = 0.05, col = "red", lty = 2, lwd = 2) # QQ plot qqplot(qunif(ppoints(length(pvals))), pvals, main = "P-value QQ Plot", xlab = "Expected (Uniform)", ylab = "Observed") abline(0, 1, col = "red", lty = 2) ``` ### Volcano Plot ```{r volcano, fig.cap="Volcano plot of activity results"} # Create volcano plot data volcano_data <- data.frame( zscore = result$zscore[, 1], pvalue = result$pvalue[, 1], neg_log_p = -log10(result$pvalue[, 1]) ) volcano_data$significant <- abs(volcano_data$zscore) > 2 & volcano_data$pvalue < 0.05 ggplot(volcano_data, aes(x = zscore, y = neg_log_p, color = significant)) + geom_point(alpha = 0.6, size = 1.5) + scale_color_manual(values = c("FALSE" = "gray70", "TRUE" = "red")) + geom_hline(yintercept = -log10(0.05), linetype = "dashed", color = "blue") + geom_vline(xintercept = c(-2, 2), linetype = "dashed", color = "blue") + labs( title = "Volcano Plot", x = "Activity Z-score", y = "-log10(P-value)", color = "Significant" ) + theme_minimal() + theme(legend.position = "bottom") ``` ## Sample Correlation ```{r sample-cor, fig.cap="Sample correlation heatmap"} # Calculate sample correlation sample_cor <- cor(result$zscore, method = "spearman") # Simple heatmap using base R heatmap(sample_cor, main = "Sample Activity Correlation", col = colorRampPalette(c("#4575B4", "white", "#D73027"))(100), symm = TRUE) ``` ## Tips for Better Visualizations ### 1. Choose Appropriate Color Palettes ```{r color-tips, fig.height=3} # Demonstrate different palettes par(mfrow = c(1, 3), mar = c(2, 2, 2, 1)) # Sequential (for magnitude) image(1:100, 1, as.matrix(1:100), col = colorRampPalette(c("white", "#D73027"))(100), main = "Sequential", xaxt = "n", yaxt = "n", xlab = "", ylab = "") # Divergent (for positive/negative) image(1:100, 1, as.matrix(-50:49), col = colorRampPalette(c("#4575B4", "white", "#D73027"))(100), main = "Divergent", xaxt = "n", yaxt = "n", xlab = "", ylab = "") # Qualitative (for categories) image(1:8, 1, as.matrix(1:8), col = RColorBrewer::brewer.pal(8, "Set2"), main = "Qualitative", xaxt = "n", yaxt = "n", xlab = "", ylab = "") ``` ### 2. Consider Your Audience - **Presentations**: Use high contrast, large labels - **Publications**: Use grayscale-friendly palettes - **Interactive**: Consider plotly for web ### 3. Always Include Context - Axis labels with units - Meaningful titles - Sample/condition annotations - Statistical thresholds ## Session Info ```{r session} sessionInfo() ```