ggplot2无法显示次要网格线问题求助
解决ggplot2瀑布图添加白色网格线区分tile的问题
问题说明
使用ggplot2绘制瀑布图时,尝试通过panel.grid.minor设置白色次要网格线区分每个tile,但始终无法生效。已尝试单独设置x、y轴网格线,均无效果。
原因分析
你的x轴(sampleID)和y轴(gene_name)都是离散型因子变量,ggplot2默认不会为离散轴生成次要网格线,因此panel.grid.minor的设置无法起作用,需要手动生成网格线来实现区分效果。
解决方案
通过geom_hline和geom_vline手动添加白色网格线,且需将网格线图层放在geom_tile之前,确保tile显示在网格线上方。
修改后的完整代码
library(ggplot2) library(tidyverse) # 读取数据(示例用内置数据,实际可替换为read.table("pd_output.txt", header = TRUE, sep = "\t")) data <- tibble( sampleID = c("P-0028", "P-0029", "P-0030", "P-0031", "P-0032", "P-0033", "P-0034", "P-0035", "P-0036"), gene_name = c("NCOR1", "SETD2", "ATM", "CDKN1B", "KMT2C", "FOXA1", "NCOR1", "KMT2A", "KMT2C"), mutation_types = factor(c("CLONAL", "CLONAL", "SUBCLONAL", "CLONAL", "CLONAL", "CLONAL", "CLONAL", "CLONAL", "CLONAL")), variant_consequences = factor(c("missense_variant", "splice_donor_variant", "stop_gained", "frameshift_variant", "stop_gained", "stop_gained", "missense_variant", "missense_variant", "missense_variant")), impact = factor(c("MODERATE", "HIGH", "HIGH", "HIGH", "HIGH", "HIGH", "MODERATE", "MODERATE", "MODERATE")), clinical_annotations = factor(c("localised", "localised", "localised", "localised", "metastatic", "metastatic", "metastatic", "localised", "localised")), TE_fusion = factor(c("no", "yes", "no", "yes", "no", "yes", "yes", "yes", "yes")), hotspot = factor(c("no", "yes", "no", "no", "no", "yes", "no", "no", "no")) ) # 创建瀑布图函数 create_waterfall_plot <- function(data) { # 获取离散轴的索引位置,用于生成网格线 x_positions <- seq_along(unique(data$sampleID)) y_positions <- seq_along(unique(data$gene_name)) plot <- ggplot(data, aes(x = sampleID, y = gene_name)) + theme_bw() + # 添加白色分隔网格线(刚好落在tile之间) geom_vline(xintercept = x_positions + 0.5, color = "white", size = 2) + geom_hline(yintercept = y_positions + 0.5, color = "white", size = 2) + theme( panel.grid.major = element_blank(), panel.grid.minor = element_blank(), # 关闭默认次要网格避免冲突 axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5) ) + geom_tile(aes(fill = variant_consequences, colour = mutation_types, alpha = 0.5), size = 0.5, width = 0.8, height = 0.8) + geom_point(aes(shape = mutation_types, colour = impact), size = 3) + scale_fill_manual(values = c( "missense_variant" = "blue", "splice_donor_variant" = "orange", "stop_gained" = "darkgreen", "frameshift_variant" = "yellow", "inframe_deletion" = "brown", "missense_variant&splice_region_variant" = "violet", "stop_gained & inframe_deletion" = "gray", "inframe_insertion" = "cyan" )) + scale_color_manual(values = c("MODERATE" = "lightpink", "HIGH" = "red")) + labs( x = "Sample ID", y = "Gene Name", fill = "Variant Consequences", colour = "Impact", shape = "CLONALITY" ) + guides(alpha = FALSE) return(plot) } # 生成并显示瀑布图 waterfall_plot <- create_waterfall_plot(data) print(waterfall_plot)
关键修改点
- 网格线位置计算:用
seq_along()获取离散轴的索引,通过索引+0.5确定线条位置,刚好落在相邻tile的分界处。 - 图层顺序:将网格线图层放在
geom_tile之前,确保网格线不会被tile遮挡。 - 关闭默认网格:设置
panel.grid.minor = element_blank(),避免默认网格与手动添加的线条冲突。
内容的提问来源于stack exchange,提问作者Venkatesh Chellappa
相关产品推荐
相关产品推荐

