如何在R语言中结合ggballoonplot添加树状图并重构气泡图
Great question! While ggballoonplot doesn’t have built-in dendrogram support like corrplot, you can absolutely add one and reorder your plot based on clustering by combining basic clustering steps with ggplot2’s flexibility. Here’s a step-by-step solution tailored to your use case:
Step 1: Prepare Data and Run Clustering
First, we’ll cluster either your marker (y-axis) or cluster (x-axis) groups using the expression data, then extract the sorted order from the dendrogram. Let’s use marker groups as an example:
# Load required packages library(tidyverse) library(ggpubr) # For ggballoonplot library(ggdendro) library(gridExtra) # Simulate a dataset matching your structure (skip if you have your own data) set.seed(123) X <- expand.grid(cluster = paste0("Cluster_", 1:4), marker = paste0("Marker_", 1:6)) X$Relative.Expression <- runif(nrow(X), 0, 1) X$Median.Intensity <- sample(c("Low", "Medium", "High"), nrow(X), replace = TRUE) # Convert data to wide format for clustering (markers as rows, clusters as columns) wide_data <- X %>% pivot_wider(names_from = cluster, values_from = Relative.Expression) %>% column_to_rownames("marker") # Run hierarchical clustering dist_matrix <- dist(wide_data, method = "euclidean") hc <- hclust(dist_matrix, method = "ward.D2") # Extract sorted marker order from the dendrogram sorted_markers <- rownames(wide_data)[hc$order] # Convert marker to a factor with the sorted order (this reorders the y-axis) X$marker <- factor(X$marker, levels = sorted_markers)
Step 2: Plot the Reordered Balloon Plot
Now use ggballoonplot with the sorted factor levels to get your reordered bubble chart:
p_balloon <- ggballoonplot(X, x = "cluster", y = "marker", size = "Relative.Expression", fill = "Median.Intensity", ggtheme = theme_classic()) + theme(axis.text.y = element_text(size = 10), plot.margin = margin(5, 10, 5, 5)) # Adjust margin to align with dendrogram
Step 3: Generate and Combine the Dendrogram
Use ggdendro to create a ggplot-compatible dendrogram, then combine it with your balloon plot using grid.arrange:
# Create rotated dendrogram (matches the y-axis of the balloon plot) dendro_plot <- ggdendrogram(hc, rotate = TRUE, theme_dendro = FALSE) + theme_void() + theme(plot.margin = margin(5, 0, 5, 10)) # Align margin with the balloon plot # Combine the two plots side-by-side grid.arrange(dendro_plot, p_balloon, ncol = 2, widths = c(1, 4))
For Clustering Clusters (X-axis)
If you want to reorder the cluster groups instead, follow the same logic but adjust the clustering and dendrogram orientation:
# Cluster clusters instead of markers wide_data_clusters <- X %>% pivot_wider(names_from = marker, values_from = Relative.Expression) %>% column_to_rownames("cluster") dist_clusters <- dist(wide_data_clusters, method = "euclidean") hc_clusters <- hclust(dist_clusters, method = "ward.D2") sorted_clusters <- rownames(wide_data_clusters)[hc_clusters$order] # Update cluster factor order X$cluster <- factor(X$cluster, levels = sorted_clusters) # Balloon plot with sorted clusters p_balloon_x <- ggballoonplot(X, x = "cluster", y = "marker", size = "Relative.Expression", fill = "Median.Intensity", ggtheme = theme_classic()) + theme(axis.text.x = element_text(angle = 45, hjust = 1), plot.margin = margin(10, 5, 5, 5)) # Top-aligned dendrogram dendro_plot_x <- ggdendrogram(hc_clusters, rotate = FALSE, theme_dendro = FALSE) + theme_void() + theme(plot.margin = margin(5, 5, 0, 5)) # Combine plots vertically grid.arrange(dendro_plot_x, p_balloon_x, nrow = 2, heights = c(1, 4))
The core idea here is to first use clustering to define the order of your groups, convert those groups to factors with the sorted order, then build the dendrogram as a separate ggplot object and combine it with the balloon plot. This gives you the same clustered ordering effect as corrplot, but with the full flexibility of ggplot2.
内容的提问来源于stack exchange,提问作者startswithH

