ggplot中stat_summary折线无法显示问题求助
解决ggplot因子型x轴无法渲染折线的问题
你的代码有两个关键问题导致折线无法显示,修改后无需转换Variable类型即可正常生成折线:
问题1:geom参数大小写错误
ggplot中所有geom的名称都是小写的,你写的geom="Line"会被识别为无效的geom类型,因此折线不会被绘制,需要改为geom="line"。
问题2:缺少group映射
当x轴变量是字符型因子时,需要显式在aes()中指定group=MainThemes,确保ggplot知道按照哪个分组来连接每个Variable对应的均值点。
修改后的完整代码
ggplot(DF_Compound, aes(color=MainThemes, y=value, x=Variable, group=MainThemes)) + stat_summary( fun.y="mean", geom="point", size=2, ) + stat_summary( fun.y="mean", geom="line", # 改为小写line size=1, ) + ylim(0,9) + ylab("") + scale_y_continuous(breaks = c(1,2,3,4,5,6,7)) + theme_minimal() + scale_color_discrete(guide = guide_legend()) + # 对应color映射,替换原scale_fill_discrete theme(legend.position="bottom")
额外说明
你代码中的scale_fill_discrete是无效的,因为你用的是color=MainThemes(点和线的颜色映射)而非填充映射,所以改为scale_color_discrete才能正确控制图例。
内容的提问来源于stack exchange,提问作者Jan
相关产品推荐
相关产品推荐

