使用ggplot绘图调用scale_*_manual后图例仍不显示问题求助
问题根因
- 你在
geom_line()、geom_point()的aes()内部声明了color映射后,又在函数外部重复指定了固定color值,这会直接覆盖映射逻辑,ggplot不会为硬编码的美学属性生成图例。 - 你所有分组用的是
color美学,但最后调用的是scale_fill_discrete(),fill针对的是填充类元素(如柱状图的柱身),和你使用的线条、点的color属性完全不对应,无法生效。 - 你在aes中传入的color值是色号,不是菌株类别名称,就算生成图例也会显示色号而非你需要的菌株名。
修正代码
最小改动版本(无需修改数据结构)
ggplot(data = OD)+ theme_light()+ # 去掉geom外部的硬编码color,aes内color传入菌株名 geom_line(aes(x=Days, y=Wildtype, group=1, color="Wildtype"), linetype="solid")+ geom_point(aes(x=Days, y=Wildtype, group=1, color="Wildtype"), shape=15, size=1.5)+ geom_errorbar(aes(x=Days, y=Wildtype, ymin=Wildtype-SD, ymax=Wildtype+SD),width=.2, position=position_dodge(0.05))+ geom_line(aes(x=Days, y=Winter, group=1, color="Winter"), linetype="solid")+ geom_point(aes(x=Days, y=Winter, group=1, color="Winter"), shape=15, size=1.5)+ geom_errorbar(aes(x=Days, y=Winter, ymin=Winter-SD.1, ymax=Winter+SD.1),width=.2, position=position_dodge(0.05))+ geom_line(aes(x=Days, y=Flagella_less, group=1, color="Flagella_less"), linetype="solid")+ geom_point(aes(x=Days, y=Flagella_less, group=1, color="Flagella_less"), shape=15, size=1.5)+ geom_errorbar(aes(x=Days, y=Flagella_less, ymin=Flagella_less-SD.2, ymax=Flagella_less+SD.2),width=.2, position=position_dodge(0.05))+ labs(title="Growth curve",x="Days",y="OD750",color="菌株")+ theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5,color="black",size=8), axis.text.y=element_text(angle=0,hjust=1,vjust=0.5,color="black",size=8), plot.title=element_text(hjust=0.5, size=12,face = "bold",margin = margin(t=0, r=10,b=10,l=10)), axis.title.y =element_text(size=10, margin=margin(t=0,r=10,b=0,l=0)), axis.title.x =element_text(size=10, margin=margin(t=10,r=10,b=0,l=0)), legend.position = "right")+ # 替换为color对应的标度,手动指定颜色 scale_color_manual(values = c("Wildtype" = "darkorange2", "Winter" = "cadetblue3", "Flagella_less" = "olivedrab3"))
更规范的版本(先转长格式数据,代码更简洁易维护)
library(tidyr) library(dplyr) # 宽数据转长数据 OD_long <- OD %>% select(Days, Wildtype, Winter, Flagella_less, SD, SD.1, SD.2) %>% pivot_longer(cols = c(Wildtype, Winter, Flagella_less), names_to = "Strain", values_to = "OD_value") %>% mutate(SD_value = case_when( Strain == "Wildtype" ~ SD, Strain == "Winter" ~ SD.1, Strain == "Flagella_less" ~ SD.2 )) # 绘图代码 ggplot(OD_long, aes(x = Days, y = OD_value, color = Strain, group = Strain)) + theme_light() + geom_line(linetype = "solid") + geom_point(shape = 15, size = 1.5) + geom_errorbar(aes(ymin = OD_value - SD_value, ymax = OD_value + SD_value), width = 0.2, position = position_dodge(0.05)) + labs(title = "Growth curve", x = "Days", y = "OD750", color = "菌株") + theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5,color="black",size=8), axis.text.y=element_text(angle=0,hjust=1,vjust=0.5,color="black",size=8), plot.title=element_text(hjust=0.5, size=12,face = "bold",margin = margin(t=0, r=10,b=10,l=10)), axis.title.y =element_text(size=10, margin=margin(t=0,r=10,b=0,l=0)), axis.title.x =element_text(size=10, margin=margin(t=10,r=10,b=0,l=0)), legend.position = "right") + scale_color_manual(values = c("Wildtype" = "darkorange2", "Winter" = "cadetblue3", "Flagella_less" = "olivedrab3"))
内容的提问来源于stack exchange,提问作者paula
相关产品推荐
相关产品推荐

