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

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

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:
我的数据如下:

  1. emean_Sleep_State <- c('WAKE','SWS','REM','WAKE','SWS','REM','WAKE','SWS','REM')
  2. emean_Treatment <- c('Disturbed','Disturbed','Disturbed','Recovery','Recovery','Recovery','UnDisturbed','UnDisturbed','UnDisturbed')
  3. emean_Means <- c(31.2,48.8,23.2,28.2,50.1,24.1,27.7,49.9,24.2)
  4. emean_SE <- c(2.91,2.80,2.80,2.87,2.74,2.78,2.92,2.81,2.81)
  5. emeanframe <- data.frame(emean_Sleep_State,emean_Treatment,emean_Means,emean_SE)
  6. Estimated_Max_Means_emean <- emeanframe$emean_Means + emeanframe$emean_SE
  7. Estimated_Min_Means_emean <- emeanframe$emean_Means - emeanframe$emean_SE
  8. emeanframe <- data.frame(emean_Sleep_State,emean_Treatment,emean_Means,emean_SE,Estimated_Max_Means_emean,Estimated_Min_Means_emean)
  9. realvalueemean <- apply(emeanframe[,c(3,5,6)],2,FUN=backtransANG)
  10. emeanframe$realmean <- realvalueemean[,1]
  11. emeanframe$realmaxmean <- realvalueemean[,2]
  12. emeanframe$realminmean <- realvalueemean[,3]

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

  1. emeanplot <- ggplot(as.data.frame(emeanframe$'emean_Treatment'),aes(x = factor(emeanframe$'emean_Sleep_State'),emeanframe$'realmean',color=emeanframe$'emean_Treatment'))+
  2. 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) +
  3. scale_x_discrete(limits = c('WAKE','SWS','REM')) +
  4. geom_errorbar(aes(ymin=emeanframe$'realminmean',ymax=emeanframe$'realmaxmean'),width=0.2,position = position_dodge(width = 0.9),linewidth=1) +
  5. scale_color_manual(values = c('black','black','black'))+
  6. 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:

  1. emean_Sleep_State <- c('WAKE','SWS','REM','WAKE','SWS','REM','WAKE','SWS','REM')
  2. emean_Treatment <- c('Disturbed','Disturbed','Disturbed','Recovery','Recovery','Recovery','UnDisturbed','UnDisturbed','UnDisturbed')
  3. emean_Means <- c(31.2,48.8,23.2,28.2,50.1,24.1,27.7,49.9,24.2)
  4. emean_SE <- c(2.91,2.80,2.80,2.87,2.74,2.78,2.92,2.81,2.81)
  5. emeanframe <- data.frame(emean_Sleep_State,emean_Treatment,emean_Means,emean_SE)
  6. Estimated_Max_Means_emean <- emeanframe$emean_Means + emeanframe$emean_SE
  7. Estimated_Min_Means_emean <- emeanframe$emean_Means - emeanframe$emean_SE
  8. emeanframe <- data.frame(emean_Sleep_State,emean_Treatment,emean_Means,emean_SE,Estimated_Max_Means_emean,Estimated_Min_Means_emean)
  9. realvalueemean <- apply(emeanframe[,c(3,5,6)],2,FUN=backtransANG)
  10. emeanframe$realmean <- realvalueemean[,1]
  11. emeanframe$realmaxmean <- realvalueemean[,2]
  12. emeanframe$realminmean <- realvalueemean[,3]

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

  1. emeanplot <- ggplot(as.data.frame(emeanframe$'emean_Treatment')
  2. ,aes(x = factor(emeanframe$'emean_Sleep_State'),emeanframe$'realmean'
  3. ,color=emeanframe$'emean_Treatment'))+
  4. 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)) +
  5. scale_x_discrete(limits = c('WAKE','SWS','REM')) +
  6. geom_errorbar(aes(ymin=emeanframe$'realminmean'
  7. ,ymax=emeanframe$'realmaxmean')
  8. ,width=0.2,position = position_dodge(width = 0.9),linewidth=1) +
  9. scale_color_manual(values = c('black','black','black'))+
  10. 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

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

  1. library(ggplot2)
  2. ggplot(emeanframe,
  3. aes(x = emean_Sleep_State,
  4. y = realmean,
  5. fill = emean_Treatment)) +
  6. geom_col(position = 'dodge',
  7. colour = "black",
  8. alpha = 0.5) + # this is what makes the errorbars visible!
  9. geom_errorbar(aes(ymin = realminmean,
  10. ymax = realmaxmean,
  11. colour = emean_Treatment),
  12. width = 0.2,
  13. position = position_dodge(width = 0.9),
  14. linewidth = 1) +
  15. scale_x_discrete(limits = c('WAKE','SWS','REM')) +
  16. labs(x = 'Sleep State',
  17. y = 'Proportion of Behaviour',
  18. fill = "Type of Night",
  19. color = "Type of Night") +
  20. # ylim(0, 1) + # commented out because I dont have the backtransANG function that I currently replaced with I()
  21. theme(axis.text.x = element_text(size = 12),
  22. axis.text.y = element_text(size = 12),
  23. axis.title.x = element_text(size = 14),
  24. axis.title.y = element_text(size = 14))

这是代码的翻译。

英文:

Solution

You could solve this way:

  1. library(ggplot2)
  2. ggplot(emeanframe,
  3. aes(x = emean_Sleep_State,
  4. y = realmean,
  5. fill = emean_Treatment)) +
  6. geom_col(position = 'dodge',
  7. colour = "black",
  8. alpha = 0.5) + # this is what makes the errorbars visible!
  9. geom_errorbar(aes(ymin = realminmean,
  10. ymax = realmaxmean,
  11. colour = emean_Treatment),
  12. width = 0.2,
  13. position = position_dodge(width = 0.9),
  14. linewidth = 1) +
  15. scale_x_discrete(limits = c('WAKE','SWS','REM')) +
  16. labs(x = 'Sleep State',
  17. y = 'Proportion of Behaviour',
  18. fill = "Type of Night",
  19. color = "Type of Night") +
  20. # ylim(0, 1) + # commented out because I dont have the backtransANG function that I currently replaced with I()
  21. theme(axis.text.x = element_text(size = 12),
  22. axis.text.y = element_text(size = 12),
  23. axis.title.x = element_text(size = 14),
  24. 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:

  1. ggplot(emeanframe,
  2. aes(x = emean_Sleep_State,
  3. y = realmean,
  4. fill = emean_Treatment)) +
  5. geom_col(position = 'dodge',
  6. colour = "black") +
  7. geom_errorbar(aes(ymin = realminmean,
  8. ymax = realmaxmean),
  9. colour = "black",
  10. width = 0.2,
  11. position = position_dodge(width = 0.9),
  12. linewidth = 1) +
  13. scale_x_discrete(limits = c('WAKE','SWS','REM')) +
  14. labs(x = 'Sleep State',
  15. y = 'Proportion of Behaviour',
  16. color = "Type of Night") +
  17. # ylim(0, 1) + # commented out because I dont have the backtransANG function that I currently replaced with I()
  18. theme(axis.text.x = element_text(size = 12),
  19. axis.text.y = element_text(size = 12),
  20. axis.title.x = element_text(size = 14),
  21. 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:

  1. 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:

确定