如何修改R中gbm_pheatmap包装函数的绘图参数(解决注释重叠)
gbm_pheatmap from cellrangerRkit Got it, let's tackle that overlapping annotation text issue in gbm_pheatmap. Since the function doesn't expose fontsize parameters for the annotations, we have a few straightforward ways to adjust this behavior:
Option 1: Modify the existing gbm_pheatmap function
You can directly tweak the original function to add support for annotation fontsize controls. Here's how:
- Start with the source code of
gbm_pheatmap(you already have this, but you can re-fetch it anytime withprint(gbm_pheatmap)in R). - Add fontsize-related parameters to the function definition, then pass them to the underlying
pheatmap()call at the end.
Here's the modified function with added fontsize controls:
gbm_pheatmap <- function (gbm, genes_to_plot, cells_to_plot, n_genes = 5, colour = NULL, limits = c(-3, 3), annotation_fontsize = 8, fontsize = 10) { # New fontsize parameters added if (!is.list(genes_to_plot)) { cat("Plotting one gene set instead of multiple cluster-specific gene sets\n") gene_indices <- sapply(genes_to_plot, function(x) get_gene_index(gbm, x)) gene_annotation <- NULL } else { if ("significant" %in% names(genes_to_plot[[1]])) { gene_indices <- unlist(lapply(genes_to_plot, function(x) with(x, head(ix[significant], n_genes)))) gene_grouping <- unlist(lapply(names(genes_to_plot), function(nm) rep(nm, with(genes_to_plot[[nm]], length(head(ix[significant], n_genes)))))) } else { gene_indices <- unlist(lapply(genes_to_plot, function(x) x$ix[1:n_genes])) gene_grouping <- rep(names(genes_to_plot), each = n_genes) } gene_annotation <- data.frame(ClusterID = as.factor(gene_grouping)) } cell_indices <- unlist(lapply(cells_to_plot, function(x) x$ix)) value <- t(scale(t(as.matrix(exprs(gbm))[gene_indices, cell_indices]))) value[value < limits[1]] <- limits[1] value[value > limits[2]] <- limits[2] rownames(value) <- make.unique(fData(gbm)$symbol[gene_indices]) cell_grouping <- unlist(lapply(1:length(cells_to_plot), function(x) { rep(names(cells_to_plot)[x], length(cells_to_plot[[x]]$barcode)) })) cell_annotation <- data.frame(ClusterID = as.factor(cell_grouping)) rownames(cell_annotation) <- colnames(value) if (!is.null(gene_annotation)) { rownames(gene_annotation) <- rownames(value) } if (is.null(colour)) { anno_colors <- NULL } else { names(colour) <- names(cells_to_plot) anno_colors <- list(ClusterID = colour) } # Pass new fontsize parameters to the pheatmap call pheatmap(value, cluster_rows = FALSE, cluster_cols = FALSE, show_colnames = FALSE, annotation_row = gene_annotation, annotation_col = cell_annotation, annotation_names_row = FALSE, annotation_names_col = FALSE, annotation_colors = anno_colors, annotation_fontsize = annotation_fontsize, # Added parameter fontsize = fontsize) # Added parameter }
Now when you call gbm_pheatmap(), you can adjust annotation_fontsize to a smaller value (like annotation_fontsize = 6) to fix the text overlap.
Option 2: Create a custom wrapper function
If you don't want to overwrite the original gbm_pheatmap, just rename the modified function above to something like custom_gbm_pheatmap. This keeps the original function intact while letting you use your adjusted version for plotting.
Option 3: Temporarily adjust pheatmap's global defaults
For a quick fix without modifying the function, you can set global defaults for pheatmap before calling gbm_pheatmap:
# Set default annotation fontsize to a smaller value options(pheatmap.defaults = list(annotation_fontsize = 8)) # Run your gbm_pheatmap call as usual gbm_pheatmap(...) # Optional: Reset defaults after plotting if needed options(pheatmap.defaults = NULL)
This works because pheatmap uses these global defaults when parameters aren't explicitly passed in the function call.
内容的提问来源于stack exchange,提问作者Atakan

