ggplot2图例颜色与条形图不匹配问题求助
问题:ggplot图例与条形颜色不匹配及误差线设置问题
将图表条形从灰色带彩色边框改为实心填充后,因颜色相同导致半数误差线不可见,遂将误差线改为黑色提升对比度,但图例项全部变为黑色。尝试scale_color_manual、scale_fill_manual等多种参数组合,均无法让图例匹配条形颜色,还出现条形堆叠、误差线消失等问题。
数据代码
emean_Sleep_State <- c('WAKE','SWS','REM','WAKE','SWS','REM','WAKE','SWS','REM') emean_Treatment <- c('Disturbed','Disturbed','Disturbed','Recovery','Recovery','Recovery','UnDisturbed','UnDisturbed','UnDisturbed') emean_Means <- c(31.2,48.8,23.2,28.2,50.1,24.1,27.7,49.9,24.2) emean_SE <- c(2.91,2.80,2.80,2.87,2.74,2.78,2.92,2.81,2.81) emeanframe <- data.frame(emean_Sleep_State,emean_Treatment,emean_Means,emean_SE) Estimated_Max_Means_emean <- emeanframe$emean_Means + emeanframe$emean_SE Estimated_Min_Means_emean <- emeanframe$emean_Means - emeanframe$emean_SE emeanframe <- data.frame(emean_Sleep_State,emean_Treatment,emean_Means,emean_SE,Estimated_Max_Means_emean,Estimated_Min_Means_emean) realvalueemean <- apply(emeanframe[,c(3,5,6)],2,FUN=backtransANG) emeanframe$realmean <- realvalueemean[,1] emeanframe$realmaxmean <- realvalueemean[,2] emeanframe$realminmean <- realvalueemean[,3]
原绘图代码
emeanplot <- ggplot(as.data.frame(emeanframe$'emean_Treatment') ,aes(x = factor(emeanframe$'emean_Sleep_State'),emeanframe$'realmean' ,color=emeanframe$'emean_Treatment'))+ geom_bar(stat = 'identity',position = 'dodge',fill = c('#F8766D','#F8766D','#F8766D','#00BA38','#00BA38','#00BA38','#619CFF','#619CFF','#619CFF')) + theme(axis.text.x = element_text(size = 12)) + theme(axis.text.y = element_text(size = 12)) + scale_x_discrete(limits = c('WAKE','SWS','REM')) + geom_errorbar(aes(ymin=emeanframe$'realminmean' ,ymax=emeanframe$'realmaxmean') ,width=0.2,position = position_dodge(width = 0.9),linewidth=1) + scale_color_manual(values = c('black','black','black'))+ xlab('Sleep State') + ylab('Proportion of Behaviour') + labs(color = "Type of Night",size=14) + ylim(0,1) + theme(axis.title.x = element_text(size = 14)) + theme(axis.title.y = element_text(size=14))
问题根源
- 直接在
geom_bar中手动指定fill向量,未映射到emean_Treatment变量,导致图例无法关联填充色 aes中直接引用emeanframe$xxx而非数据框列名,破坏ggplot的数据映射逻辑- 用
scale_color_manual统一设置黑色,覆盖了图例的颜色映射规则
修正后的代码
# 假设backtransANG函数已提前定义 emeanplot <- ggplot(emeanframe, aes(x = factor(emean_Sleep_State), y = realmean, fill = emean_Treatment)) + geom_bar(stat = 'identity', position = 'dodge', width = 0.8) + # 误差线单独设置黑色,不加入映射 geom_errorbar(aes(ymin = realminmean, ymax = realmaxmean), width = 0.2, position = position_dodge(width = 0.8), linewidth = 1, color = "black") + # 手动指定填充色,与Treatment类别一一对应 scale_fill_manual(values = c(Disturbed = "#F8766D", Recovery = "#00BA38", UnDisturbed = "#619CFF")) + scale_x_discrete(limits = c('WAKE','SWS','REM')) + labs(x = 'Sleep State', y = 'Proportion of Behaviour', fill = "Type of Night") + ylim(0, 1) + theme( axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12), axis.title.x = element_text(size = 14), axis.title.y = element_text(size = 14), legend.title = element_text(size = 14) ) print(emeanplot)
关键修正点
- 将完整数据框传入
ggplot,aes中使用列名而非外部引用,保证映射逻辑正确 - 将
fill映射到emean_Treatment,通过scale_fill_manual指定对应颜色,让图例自动匹配条形填充色 - 误差线的
color直接设为黑色,不加入aes映射,避免干扰图例 - 统一
geom_bar和geom_errorbar的position_dodge宽度,确保误差线与条形对齐 - 合并重复的
theme设置,简化代码结构
内容的提问来源于stack exchange,提问作者e_putyora
相关产品推荐
相关产品推荐

