Geom_bar,位置闪避一个分类变量,颜色另一个

时间:2017-05-30 04:36:40

标签: r ggplot2 colors geom-bar

我想创建一个条形图,其中包含基于activity的躲避条形图和由thres指定的条形颜色,或者有条件地: cpue > 75 = 'green' cpue < 75 & cpue > 50 = 'orange' cpue < 50 = 'purple'

数据的一个例子是:

     week area_code activity  cpue  p_legal  thres thres2 
   <dbl>     <chr>    <chr>    <dbl>    <dbl>  <chr>  <chr>  
1    30      2L12  Looking      NaN      NaN  green  green  
2    31      2L12  Fishing 81.96667 90.00000  green  green  
3    31      2L12  Looking 61.32353 90.00000 orange  green 
4    32      2L12  Fishing 87.07883 80.00000  green  green  
5    32      2L12  Looking 66.99622 94.33333 orange  green 
6    35      2L12  Fishing 60.29923 86.25000 orange  green 

我知道我可以使用像:

这样的小平面包裹
ggplot(plot_df[plot_df$area_code == area, ]) +
  geom_bar(aes(x=week, y=cpue, fill=thres), stat='identity') +
  scale_fill_manual(values=c('green'= 'green', 'orange'='orange', 'purple' = 'purple'), guide=FALSE) +
  geom_hline(yintercept = c(50, 75)) +
  facet_wrap(~activity)

我真正喜欢的是躲闪酒吧,但我遇到的问题是,geom bar根据填充美感闪避条纹,颜色美学只会改变边框颜色,例如。

 ggplot(plot_df[plot_df$area_code == area, ]) +
  geom_bar(aes(x=week, y=cpue, fill=activity, colour = thres), position = 'dodge', stat='identity') +
  geom_hline(yintercept = c(50, 75))

Example of undesirable output with colour aesthetic changing border and not bar colour

我想要与上面相同的输出,但条形颜色由所列条件或thres

决定

1 个答案:

答案 0 :(得分:1)

当您已经使用interaction()作为因子时,

Facets是其他因素的首选解决方案。如果您想填充两个因素,可以尝试使用ggplot(plot_df[plot_df$area_code == area, ]) + geom_col(aes(week, cpue, fill = interaction(activity, thres)), position = "dodge")

  - name: copy the checkout.py file
    copy:
    src: /home/checkout.py
    dest: /home
    mode: 0777

enter image description here