为什么我的图例与ggplot2中的柱形颜色不匹配?

huangapple go评论137阅读模式
英文:

Why does my legend not match the colour of my bars in ggplot2?

问题

I converted the bars of my graph from gray with a coloured outline to a solid fill which resulted in half of my error bars not being visible due to being the same colour. I therefore changed the colour of the error bars to black to improve contrast but this has resulted in the items of my legend all being black and I cannot find a way to make the legend match the colour of my bars without making a complete mess of the graph (e.g. bars stacking on top of each other and error bars disappearing).

我的图表中,我将柱形图从灰色填充更改为具有彩色轮廓的实体填充,导致一半的误差线由于颜色相同而不可见。因此,我将误差线的颜色更改为黑色以改善对比度,但这导致我的图例中的项目都是黑色的,我无法找到一种方法使图例与我的柱形图的颜色匹配,而不会让图表混乱(例如,柱形图重叠和误差线消失)。

My data is as follows:
我的数据如下:

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]

Similarly, the code I used to create my graphs is:
同样,我用于创建图表的代码如下:

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))

I'm not sure exactly what I am doing wrong so any help would be greatly appreciated!
我不确定我到底做错了什么,所以非常感谢任何帮助!
I have tried some permutations of scale_color_manual, scale_fill_manual, colour=c(''), but not have resulted in a solution. Namely, my legend is still 3 black boxes which do not match my bars which are red, green and blue.
我尝试过一些scale_color_manual,scale_fill_manual,color=c('')的排列组合,但没有找到解决方案。换句话说,我的图例仍然是3个黑色方框,与我的柱形图不匹配,而柱形图是红色、绿色和蓝色。

英文:

I converted the bars of my graph from gray with a coloured outline to a solid fill which resulted in half of my error bars not being visible due to being the same colour. I therefore changed the colour of the error bars to black to improve contrast but this has resulted in the items of my legend all being black and I cannot find a way to make the legend match the colour of my bars without making a complete mess of the graph (e.g. bars stacking on top of each other and error bars disappearing).

My data is as follows:

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]

Similarly, the code I used to create my graphs is:

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))

I'm not sure exactly what I am doing wrong so any help would be greatly appreciated!

I have tried some permutations of scale_color_manual, scale_fill_manual, colour=c('') but not have resulted in a solution. Namely, my legend is still 3 black boxes which do not match my bars which are red, green and blue.

答案1

得分: 0

以下是翻译好的代码部分:

library(ggplot2)

ggplot(emeanframe,
       aes(x = emean_Sleep_State,
           y = realmean,
           fill = emean_Treatment)) +
  
  geom_col(position = 'dodge', 
           colour = "black", 
           alpha = 0.5) + # this is what makes the errorbars visible!
  geom_errorbar(aes(ymin = realminmean,
                    ymax = realmaxmean,
                    colour = emean_Treatment),
                width = 0.2,
                position = position_dodge(width = 0.9),
                linewidth = 1) + 
  
  scale_x_discrete(limits = c('WAKE','SWS','REM')) + 
  labs(x = 'Sleep State',
       y = 'Proportion of Behaviour',
       fill = "Type of Night",
       color = "Type of Night") +
  # ylim(0, 1) + # commented out because I dont have the backtransANG function that I currently replaced with I()
  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))

这是代码的翻译。

英文:

Solution

You could solve this way:

library(ggplot2)

ggplot(emeanframe,
       aes(x = emean_Sleep_State,
           y = realmean,
           fill = emean_Treatment)) +
  
  geom_col(position = 'dodge', 
           colour = "black", 
           alpha = 0.5) + # this is what makes the errorbars visible!
  geom_errorbar(aes(ymin = realminmean,
                    ymax = realmaxmean,
                    colour = emean_Treatment),
                width = 0.2,
                position = position_dodge(width = 0.9),
                linewidth = 1) + 
  
  scale_x_discrete(limits = c('WAKE','SWS','REM')) + 
  labs(x = 'Sleep State',
       y = 'Proportion of Behaviour',
       fill = "Type of Night",
       color = "Type of Night") +
  # ylim(0, 1) + # commented out because I dont have the backtransANG function that I currently replaced with I()
  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))

为什么我的图例与ggplot2中的柱形颜色不匹配?

I cleaned up a bit your code because you are not supposed to use $ to call the right columns inside of aes.

The important change I made to solve your problem is to add alpha = 0.5 into geom_col (geom_col is literally geom_bar when stat = 'identity')

What else I changed:

  • I removed fill in geom_bar (now geom_col) because it wasn't necessary since I replaced colour in the first aes with fill
  • I wrote colour = "black" to draw a line around the bars
  • I wrote colour = emean_Treatment to colour the errorbar like the bars
  • scale_colour_manual wasn't necessary anymore
  • I set all the labels into labs (and add fill)
  • I merged together all the themes

Alternative

Alternatively, if you just want the legend to match the bars and you want the errorbars in black, you can use this code:

ggplot(emeanframe,
       aes(x = emean_Sleep_State,
           y = realmean,
           fill = emean_Treatment)) +
  
  geom_col(position = 'dodge',
           colour = "black") +
  geom_errorbar(aes(ymin = realminmean,
                    ymax = realmaxmean),
                colour = "black",
                width = 0.2,
                position = position_dodge(width = 0.9),
                linewidth = 1) + 
  
  scale_x_discrete(limits = c('WAKE','SWS','REM')) + 
  labs(x = 'Sleep State',
       y = 'Proportion of Behaviour',
       color = "Type of Night") +
  # ylim(0, 1) + # commented out because I dont have the backtransANG function that I currently replaced with I()
  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))

为什么我的图例与ggplot2中的柱形颜色不匹配?

Here:

  • I set colour = "black" into geom_errorbar
  • I removed alpha = 0.5 because it was no longer needed

How I created you data

I didn't know what backtransANG was so I just wrote like this:

realvalueemean <- apply(emeanframe[,c(3,5,6)], 2, FUN = I)

In your plot you may want to uncomment ylim(0,1) +

huangapple
  • 本文由 发表于 2023年3月21日 00:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定