使用gghighlight包标注点时如何保留图表图例?
解决gghighlight标注后图例消失的问题
你遇到的问题是gghighlight开启use_direct_label后,图例被隐藏的情况。这是因为gghighlight在处理高亮和标注时,会修改图层的图例映射属性,加上你把未高亮点设为完全透明,进一步干扰了图例的生成。下面提供两种可靠的解决方案:
方案一:拆分图层+ggrepel标注(更稳定)
放弃依赖gghighlight的标注功能,改用ggrepel包添加点标注,同时拆分图层保留图例映射:
完整代码示例
# 先构造你的数据框 df <- data.frame( rowname = 1:6, Class = c("Formin", "Formin", "Septin", "Septin", "Arp2/3", "Arp2/3"), Sec = c("HAI", "PMA", "HAI", "PMA", "PMA", "HAI"), ES.2um = c(113.37340, 43.90661, 64.32138, 53.15791, 68.67222, 71.00643), Mean_WPBs = c(147.1792, 121.9017, 132.7591, 145.7871, 161.0516, 149.0704), ES.2um_ZS = c(0.16078492, -0.11594028, -0.16218581, -0.86969449, -0.05404113, -0.38119473), Mean_ES = c(131.69309, 75.37296, 66.23765, 81.92690, 82.51804, 82.91458), VWF_Sec = c(112.5219, 137.4212, 150.9011, 140.2647, 158.2623, 130.5494), name = c("DIAPH1", "FMN2", "SEPTIN5", "LRCH3", "ARPC3", "WASF3") ) # 计算所需均值 mean_VWF <- mean(df$VWF_Sec) mean_ES.Z <- mean(df$ES.2um_ZS) library(ggplot2) library(ggrepel) plot_ll <- ggplot() + # 1. 绘制非Formin点(透明,仅保留颜色映射以生成图例) geom_point(data = df[df$Class != "Formin", ], aes(ES.2um_ZS, VWF_Sec, col = Sec), size = 2.5, alpha = 0) + # 2. 绘制Formin高亮点(正常显示颜色) geom_point(data = df[df$Class == "Formin", ], aes(ES.2um_ZS, VWF_Sec, col = Sec), size = 2.5) + # 3. 添加Formin点的名称标注(自动避免重叠) geom_text_repel(data = df[df$Class == "Formin", ], aes(ES.2um_ZS, VWF_Sec, label = name), size = 4, nudge_x = 0.1) + # 原图表的其他设置 labs(col = "Secretaogue") + xlim(-1.25, 0.7) + ylim(70, 178) + scale_colour_manual(values = c("HAI" = "blue", "PMA" = "red")) + ggtitle("Formin Proteins Highlighted") + theme_bw() + theme(plot.title = element_text(hjust = 0.5)) + xlab("Mean Exit Site Z-Score (Area >2um)") + ylab("Secretion") + geom_hline(yintercept = mean_VWF, color = "black", linetype = "dashed") + annotate("text", -1.2, 173, label = "Mean", color = "black") + geom_vline(xintercept = mean_ES.Z, color = "black", linetype = "dashed") + annotate("text", 0.5, 70, label = "Mean", color = "black") plot_ll
原理说明
第一个透明图层保留了Sec列的颜色映射关系,让ggplot能正常生成图例;第二个图层专门绘制高亮的Formin点;用ggrepel的geom_text_repel添加标注,完全不会干扰图例的显示。
方案二:调整gghighlight参数(保留原工作流)
如果你想继续使用gghighlight的标注功能,可以通过参数调整强制保留图例:
完整代码示例
library(ggplot2) library(gghighlight) plot_ll <- ggplot(df, aes(ES.2um_ZS, VWF_Sec, col = Sec)) + geom_point(size = 2.5) + labs(col = "Secretaogue") + xlim(-1.25, 0.7) + ylim(70, 178) + scale_colour_manual(values = c("HAI" = "blue", "PMA" = "red")) + # 开启keep_scales保留原始颜色映射,修正标注后图例消失的问题 gghighlight(Class == "Formin", use_direct_label = TRUE, label_key = name, unhighlighted_colour = alpha("green", 0.0), keep_scales = TRUE) + ggtitle("Formin Proteins Highlighted") + theme_bw() + theme(plot.title = element_text(hjust = 0.5)) + xlab("Mean Exit Site Z-Score (Area >2um)") + ylab("Secretion") + geom_hline(yintercept = mean_VWF, color = "black", linetype = "dashed") + annotate("text", -1.2, 173, label = "Mean", color = "black") + geom_vline(xintercept = mean_ES.Z, color = "black", linetype = "dashed") + annotate("text", 0.5, 70, label = "Mean", color = "black") + # 强制显示图例,并修正图例点的透明度 guides(colour = guide_legend(override.aes = list(alpha = 1))) plot_ll
原理说明
keep_scales = TRUE:让gghighlight保留原始的颜色映射规则,避免图例被隐藏。guides(colour = guide_legend(override.aes = list(alpha = 1))):修正图例点的透明度,因为未高亮点设为透明后,图例点也会被影响,这里强制设为不透明。
内容的提问来源于stack exchange,提问作者Christopher Robinson
相关产品推荐
相关产品推荐

