ggplot图表出现重复标签问题:为何生成“幽灵数据”?
ggplot重复标签(幽灵数据)问题排查
问题描述
使用ggplot绘制图表时出现数据框中不存在的重复标签,例如Engineering专业的Rubric指标仅有1个值为2.1的数据点,但图表中该位置显示了3次2.1标签。确认数据框本身无额外数据,需要排查原因。
绘图代码
X <- ggplot(methods_per_CONSM_majors |> mutate(Majors = factor(Majors) |> fct_inorder(), Measures = factor(Measures) |> fct_rev()), aes(Majors, Measures, label = LCOs, color = Years)) + geom_text(size = 2.5) + geom_text_repel(box.padding = 1, max.overlaps = 5) + scale_y_discrete(guide = guide_axis(n.dodge = 8)) + labs(title = "TEMP:", subtitle = "TEMP") + xlab(label = "") + ylab(label = "") + scale_y_discrete(labels = function(x) str_wrap(x, width = 10)) + theme_minimal() + theme(panel.grid = element_line( size = (0.1), colour = "lightgrey")) + theme(legend.position = "top") + guides(color = guide_legend(nrow = 1)) + theme(axis.text.x=element_text(angle=90, hjust=1)) + geom_text_repel() X + labs(color=NULL) + scale_color_manual(values=c("gold3", "firebrick2", "blue", 'orchid', 'black'))
对应数据集
methods_per_CONSM_majors <- data.frame( 'Majors'=c('Science (Pre-Nursing)', 'Science (Pre-Nursing)', 'Science (Pre-Nursing)', 'Science (Pre-Nursing)', 'Science (Pre-Nursing)', 'Science (Pre-Nursing)', 'Science (Pre-Nursing)', 'Science (Pre-Nursing)', 'Science (Pre-Nursing)', 'Science (Pre-Nursing)', 'Engineering', 'Engineering', 'Biology', 'Biology', 'Biology', 'Biology', 'Biology', 'Biology', 'Biology', 'Biology', 'Biology', 'Biology', 'Cell Biology', 'Cell Biology', 'Cell Biology', 'Cell Biology', 'Cell Biology', 'Cell Biology', 'Cell Biology', 'Cell Biology', 'Cell Biology', 'Cell Biology', 'Macrobiology', 'Macrobiology', 'Macrobiology', 'Macrobiology', 'Macrobiology', 'Macrobiology', 'Macrobiology', 'Macrobiology', 'Macrobiology', 'Macrobiology', 'Computer Information Science', 'Computer Information Science', 'Computer Information Science', 'Computer Information Science', 'Computer Information Science', 'Computer Information Science', 'Computer Information Science', 'Computer Information Science', 'Computer Information Science', 'Computer Information Science', 'Computer Science', 'Computer Science', 'Computer Science', 'Computer Science', 'Computer Science', 'Computer Science', 'Computer Science', 'Computer Science', 'Computer Science', 'Computer Science', 'Environmental Science', 'Environmental Science', 'Environmental Science', 'Environmental Science', 'Environmental Science', 'Environmental Science', 'Environmental Science', 'Environmental Science', 'Environmental Science', 'Environmental Science', 'Health Sciences', 'Health Sciences', 'Health Sciences', 'Health Sciences', 'Kinesiology', 'Kinesiology', 'Kinesiology', 'Kinesiology', 'Kinesiology', 'Kinesiology', 'Kinesiology', 'Kinesiology', 'Kinesiology', 'Kinesiology', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Mathematics', 'Natural Sciences', 'Natural Sciences', 'Natural Sciences', 'Natural Sciences', 'Natural Sciences', 'Natural Sciences', 'Natural Sciences', 'Natural Sciences', 'Natural Sciences', 'Natural Sciences', 'Natural Sciences', 'Natural Sciences', 'Natural Sciences'), 'Measures'=c('Research Project/Essay', 'Pre/Post', 'Rubric', 'Pre/Post', 'Rubric', 'Rubric', 'Pre/Post', 'Ill-Defined', 'Rubric', 'Rubric', 'Rubric', 'Exam', 'Exit Exam', 'Pre/Post', 'Quiz', 'Pre/Post', 'Pre/Post', 'Rubric', 'Exit Exam', 'Pre/Post', 'Rubric', 'Rubric', 'Exit Exam', 'Pre/Post', 'Quiz', 'Pre/Post', 'Pre/Post', 'Rubric', 'Exit Exam', 'Pre/Post', 'Rubric', 'Rubric', 'Exit Exam', 'Pre/Post', 'Quiz', 'Pre/Post', 'Pre/Post', 'Rubric', 'Exit Exam', 'Pre/Post', 'Rubric', 'Rubric', 'Rubric', 'Rubric', 'Exam', 'Rubric', 'Rubric', 'Rubric', 'Exam', 'Rubric', 'Evaluator Observation/ Assessment', 'Evaluator Observation/ Assessment', 'Rubric', 'Exams & Projects', 'Exam', 'Rubric', 'Exam', 'Rubric', 'Exam', 'Rubric', 'Evaluator Observation/ Assessment', 'Evaluator Observation/ Assessment', 'Observation/ Other', 'Ill-Defined', 'Rubric', 'Rubric', 'Rubric', 'Rubric', 'Exams (Multiple)', 'Rubric', 'Exams (Multiple)', 'Rubric', 'Rubric', 'Exam', 'Rubric', 'Exam', 'Pre/Post', 'Pre/Post', 'Rubric', 'Pre/Post', 'Rubric', 'Exam', 'Rubric', 'Rubric', 'Pre/Post', 'Rubric', 'Exam', 'Ill-Defined', 'Exam', 'Exam', 'Exam', 'Rubric', 'Rubric', 'Rubric', 'Alumni Survey', 'Journal/ Reflection', 'Rubric', 'Rubric', 'Rubric', 'Rubric', 'Rubric', 'Exam', 'Exam', 'Exam', 'Exam', 'Exam', 'Exam', 'Rubric', 'Rubric', 'Exam', 'Ill-Defined', 'Ill-Defined', 'Rubric', 'Rubric', 'Exam', 'Quiz', 'Quiz', 'Rubric', 'Exam', 'Exam', 'Quiz', 'Rubric'), 'LCOs'=c('1.1', '2.1', '1.1', '2.1', '1.2', '3.1', '2.1', '3.1', '1.2', '3.1', '2.1', '2.2', '1.1', '2.1', '1.2', '2.1', '1.2', '2.2', '1.1', '2.1', '2.2', '3.2', '1.1', '2.1', '1.2', '2.1', '1.2', '2.2', '1.1', '2.1', '2.2', '3.2', '1.1', '2.1', '1.2', '2.1', '1.2', '2.2', '1.1', '2.1', '2.2', '3.2', '2', '4', '1.1', '3.1', '2.2', '4.2', '2.1', '4.1', '3.1', '4.2', '2', '4', '1.1', '3.1', '1.1', '2.2', '2.1', '4.1', '3.1', '4.2', '1.2', '3.1', '1.1', '2.1', '1.2', '2.1', '1.2', '2.1', '1.2', '2.1', '1.1', '2.1', '1.2', '2.2', '2.2', '5.1', '1.1', '5.1', '3.1', '6.1', '4.1', '7.1', '2.1', '5.1', '3.1', '4.1', '4.2', '3.2', '3.3', '5.1', '5.3', '5.2', '1.1', '1.2', '5.3', '5.2', '5.1', '2.1', '2.2', '3.1', '3.2', '3.3', '3.1', '3.2', '3.3', '4.1', '4.2', '1.1', '1.2', '1.3', '4.1', '4.2', '2.1', '3.6', '3.1', '4.1', '1.1', '2.1', '3', '5'), 'Years'=c('2017-2018', '2017-2018', '2018-2019', '2018-2019', '2019-2020', '2019-2020', '2020-2021', '2020-2021', '2021-2022', '2021-2022', '2021-2022', '2021-2022', '2017-2018', '2017-2018', '2018-2019', '2018-2019', '2019-2020', '2019-2020', '2020-2021', '2020-2021', '2021-2022', '2021-2022', '2017-2018', '2017-2018', '2018-2019', '2018-2019', '2019-2020', '2019-2020', '2020-2021', '2020-2021', '2021-2022', '2021-2022', '2017-2018', '2017-2018', '2018-2019', '2018-2019', '2019-2020', '2019-2020', '2020-2021', '2020-2021', '2021-2022', '2021-2022', '2017-2018', '2017-2018', '2018-2019', '2018-2019', '2019-2020', '2019-2020', '2020-2021', '2020-2021', '2021-2022', '2021-2022', '2017-2018', '2017-2018', '2018-2019', '2018-2019', '2019-2020', '2019-2020', '2020-2021', '2020-2021', '2021-2022', '2021-2022', '2017-2018', '2017-2018', '2018-2019', '2018-2019', '2019-2020', '2019-2020', '2020-2021', '2020-2021', '2021-2022', '2021-2022', '2020-2021', '2020-2021', '2021-2022', '2021-2022', '2017-2018', '2017-2018', '2018-2019', '2018-2019', '2019-2020', '2019-2020', '2020-2021', '2020-2021', '2021-2022', '2021-2022', '2017-2018', '2017-2018', '2017-2018', '2018-2019', '2018-2019', '2018-2019', '2018-2019', '2018-2019', '2019-2020', '2019-2020', '2019-2020', '2019-2020', '2019-2020', '2020-2021', '2020-2021', '2020-2021', '2020-2021', '2020-2021', '2021-2022', '2021-2022', '2021-2022', '2021-2022', '2021-2022', '2017-2018', '2017-2018', '2017-2018', '2017-2018', '2017-2018', '2018-2019', '2018-2019', '2019-2020', '2019-2020', '2020-2021', '2020-2021', '2021-2022', '2021-2022'))
原因分析
- 重复调用文本图层:你的代码中连续添加了三个文本绘制图层:
geom_text(size = 2.5)、geom_text_repel(box.padding = 1, max.overlaps = 5)、geom_text_repel()。每个图层都会遍历整个数据集,在对应坐标位置绘制一次标签,所以单个数据点会被三个图层分别渲染,导致同一位置出现三次相同标签。 - 验证逻辑:查看Engineering专业Rubric指标的行,数据集中仅1条记录,但三个文本图层各绘制一次,最终显示3次2.1标签,完全符合这个逻辑。
解决方法
删除多余的文本图层调用,只保留一个即可。例如保留带参数的geom_text_repel,修改后的代码如下:
X <- ggplot(methods_per_CONSM_majors |> mutate(Majors = factor(Majors) |> fct_inorder(), Measures = factor(Measures) |> fct_rev()), aes(Majors, Measures, label = LCOs, color = Years)) + # 仅保留一个文本图层 geom_text_repel(box.padding = 1, max.overlaps = 5, size = 2.5) + scale_y_discrete(guide = guide_axis(n.dodge = 8)) + labs(title = "TEMP:", subtitle = "TEMP") + xlab(label = "") + ylab(label = "") + scale_y_discrete(labels = function(x) str_wrap(x, width = 10)) + theme_minimal() + theme(panel.grid = element_line( size = (0.1), colour = "lightgrey")) + theme(legend.position = "top") + guides(color = guide_legend(nrow = 1)) + theme(axis.text.x=element_text(angle=90, hjust=1)) X + labs(color=NULL) + scale_color_manual(values=c("gold3", "firebrick2", "blue", 'orchid', 'black'))
内容的提问来源于stack exchange,提问作者RKeithL
相关产品推荐
相关产品推荐

