Change colours of particular bars in a bar chart

后端 未结 4 1187
轮回少年
轮回少年 2020-12-01 04:47

I\'ve been creating some bar-charts and I was wondering is it possible to colour bars on a chart depending on whether they lie above or below the x-axis?

For clarifi

4条回答
  •  时光说笑
    2020-12-01 05:17

    A ggplot2 solution using geom_bar with stat_identity.

    library(ggplot2)
    ggplot(dat, aes(x= seq_along(x), y = x)) + 
      geom_bar(stat = 'identity', aes(fill = x>0), position = 'dodge', col = 'transparent') + 
      theme_bw() + scale_fill_discrete(guide = 'none') + 
      labs(x = '', y = 'NAO Index')
    

    enter image description here

    scale_fill_discrete(guide = 'none') removes the legend, position = 'dodge' stops the warning that comes from the default position = 'stack'.

提交回复
热议问题