ggplot2分面图中图例未正确显示点填充色问题
解决ggplot2图例填充色不匹配的问题
问题分析
你使用带填充色的形状(21、24)时,同时映射fill=PM和shape=KL并按KL分面,导致图例中PM类别的填充色未正确显示——图表中点的颜色正常,但图例里FT和PP都显示黑色。这是因为ggplot2默认生成图例时,没有正确将填充色关联到你的自定义颜色设置,加上分面逻辑的影响,图例元素的填充属性出现了冲突。
解决方案
方案1:手动修正图例的填充属性
通过guides()函数强制指定fill图例的填充色,确保和图表中点的颜色一致:
ggplot(test,mapping=aes(x=XValue,y=YValue,fill=PM,shape=KL))+ geom_point(size=2)+ scale_fill_manual(values=c(FT="red",PP="black"))+ scale_shape_manual(values=c(21,24))+ facet_wrap(~KL)+ guides(fill = guide_legend(override.aes = list(fill = c("red", "black"))))
或者指定图例使用统一形状(比如21),避免形状差异干扰填充色显示:
ggplot(test,mapping=aes(x=XValue,y=YValue,fill=PM,shape=KL))+ geom_point(size=2, colour = "black")+ # 可选:统一点的边框色,提升可读性 scale_fill_manual(values=c(FT="red",PP="black"))+ scale_shape_manual(values=c(21,24))+ facet_wrap(~KL)+ guides(fill = guide_legend(override.aes = list(shape = 21)))
方案2:移除重复的shape图例
由于你已经按KL分面,每个分面对应一种形状,shape图例属于冗余信息,可直接隐藏,只保留fill图例:
ggplot(test,mapping=aes(x=XValue,y=YValue,fill=PM,shape=KL))+ geom_point(size=2)+ scale_fill_manual(values=c(FT="red",PP="black"))+ scale_shape_manual(values=c(21,24))+ facet_wrap(~KL)+ guides(shape = "none")
测试数据集
test<-structure(list(XValue = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L), .Label = c("0", "1", "3", "4", "6", "8", "12", "24", "20"), class = "factor"), PM = c("FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "FT", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP", "PP"), KL = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("K1", "K2"), class = "factor"), YValue = c(3.1659596427119, 3.1219098873624, 3.14672408788822, 0.986728824528058, 0.970927123484596, 0.851697057001025, 6.18813529232518, 6.16250945274165, 6.10036718879546, 2.04585765804258, 2.01010012388677, 1.98540967919073, 1.96831567755578, 4.54473669569724, 4.51674283471371, 4.57968928259083, 2.28260907382727, 2.33546233929094, 2.21330224364371, 2.23912504349744, 2.02414383523147, 2.07670244369616, 2.05701137806227, 2.18747377389903, 2.10975270957976, 2.11196078067211, 2.08020174459454, 1.90555716664908, 1.94338771258761, 1.90454928900316, 2.34332445212985, 2.2801434791366, 2.29583570117039, 1.79373583477998, 1.7921731037672, 1.80092325112501, 1.57072891453888, 1.53583763615075, 1.53852302844113, 1.50077935623981, 2.32516428610227, 2.26667663882382, 2.2571443123735, 1.72927075549124, 1.68745803873649, 1.67100312911936, 1.65473272587735, 2.53309570110105, 2.48119569321443, 2.49075439101024, 1.679672753252, 1.56814675032307, 1.6057015152944, 1.61552779426337, 2.63579787756314, 2.60255488643132, 2.62594327021028, 1.46141497013179, 1.4200887994802, 1.41179528904952, 3.22314580592308, 3.21369507872032, 3.22968843307558, 1.6003688022661, 1.5676859833415, 1.52704069853153, 1.52699091649978, 4.11433428998552, 4.08672056128983, 4.03041446478611, 1.47582738219854, 1.55797514144288, 1.5787805691956, 1.60176966345038, 1.58144396087639, 1.49501753126975, 1.50288648673506, 1.49649216185024, 1.50002421213479)), row.names = c(NA, -79L), class = "data.frame")
内容的提问来源于stack exchange,提问作者shghm
相关产品推荐
相关产品推荐

