使用ggplot+geom_point绘制箱线图时部分点出现光晕的解决咨询
问题描述
生成叠加散点的箱线图时,部分geom_point周围出现类似光晕/背景的效果(红色和蓝色箱线图下方),需排查原因并解决。
所用数据
Control <- structure(list(Genotype = c("DMCeibo", "DMCeibo", "DMCeibo", "DMCeibo", "DMCeibo", "DMCeibo", "MSINTA415", "MSINTA415", "MSINTA415", "MSINTA415", "MSINTA415", "MSINTA415", "Saeta", "Saeta", "Saeta", "Saeta", "Saeta", "Saeta"), Treatment = c("Control", "Control", "Control", "Control", "Control", "Control", "Control", "Control", "Control", "Control", "Control", "Control", "Control", "Control", "Control", "Control", "Control", "Control"), Year = c("2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)", "2018(Wet)"), Replication = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), GrainYield = c(4674, 5250, 5387, 5144, 5276, 5397, 5244, 5411, 5436, 5679, 4704, 4810, 5315, 4309, 5477, 5239, 4916, 5861), IndividualGrainWeight = c(46.3, 46.8, 45.3, 46.3, 43.8, 45.7, 44.5, 44.5, 45, 44.5, 43, 44.3, 42, 41, 40.5, 41, 42.5, 43), GrainNumber = c(10095, 11218, 11892, 11110, 12046, 11810, 11784, 12160, 12080, 12762, 10940, 10858, 12654, 10510, 13523, 12778, 11567, 13630), Protein = c(13.2, 12.8, 12.6, 13.5, 13.5, 13.5, 15.2, 15, 14.8, 14.9, 15, 15.2, 14.6, 14, 14.3, 14.9, 14.9, 14.4)), row.names = c(1L, 5L, 9L, 13L, 17L, 21L, 25L, 29L, 33L, 37L, 41L, 45L, 49L, 53L, 57L, 61L, 65L, 69L), class = "data.frame")
原绘图代码
ggplot( Control, aes( x = Treatment, y = GrainYield, fill = Genotype, color = Genotype ) ) + geom_boxplot(aes(fill = Genotype), alpha = 0.2, lwd = 0.1, fatten = 1, position = position_dodge(0.8) ) + geom_point(shape = 21, position = position_dodge(width = 0.8), size = 0.3) + theme_bw() + scale_y_continuous(sec.axis = sec_axis(transform = ~ . * 1)) + theme(legend.position = "none") + labs(x = "Treatment", y = "Grain Yield (kg/ha)")
问题原因
你使用了shape = 21,这个形状是带填充的空心圆点:
- 边框颜色由
color映射控制(继承了全局的color = Genotype) - 内部填充颜色由
fill映射控制(同样继承了全局的fill = Genotype) - 当
size = 0.3(尺寸极小)时,边框和填充的边缘在渲染时会产生视觉上的"光晕"效果;若填充与边框颜色存在细微差异(或受alpha值影响),这种效果会更明显。
解决方法
有两种简单的修复方式:
方法1:改用实心点形状
将geom_point的shape改为19(实心圆点),该形状仅受color参数控制,不会出现双层边缘:
ggplot( Control, aes( x = Treatment, y = GrainYield, fill = Genotype, color = Genotype ) ) + geom_boxplot(aes(fill = Genotype), alpha = 0.2, lwd = 0.1, fatten = 1, position = position_dodge(0.8) ) + geom_point(shape = 19, position = position_dodge(width = 0.8), size = 0.3) + theme_bw() + scale_y_continuous(sec.axis = sec_axis(transform = ~ . * 1)) + theme(legend.position = "none") + labs(x = "Treatment", y = "Grain Yield (kg/ha)")
方法2:统一填充与边框,或取消填充
如果坚持使用shape = 21,可以指定fill为与color一致的颜色,或者将填充设为NA(仅保留边框):
# 方式A:让填充与边框同色 geom_point(shape = 21, position = position_dodge(width = 0.8), size = 0.3, fill = aes(color = Genotype)) # 方式B:取消填充,仅显示边框 geom_point(shape = 21, position = position_dodge(width = 0.8), size = 0.3, fill = NA)
内容的提问来源于stack exchange,提问作者Swati Puranik
相关产品推荐
相关产品推荐

