I'd like to create a bar plot with dodged bars based on activity
and the bar colour dictated by thres
or alternatively conditionally:
cpue > 75 = 'green'
cpue < 75 & cpue > 50 = 'orange'
cpue < 50 = 'purple'
An example of the data is:
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
I know I can use a facet wrap like:
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)
What I'd actually like is dodged bars but the problem I'm encountering is that geom bar dodges bars based on the fill aesthetic and the colour aesthetic only changes the border colour e.g.
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))
I'd like the same output as above but bar colour dictated by the condition outlined or thres
Facets is the preferred solution for additional factors, when you have already used fill
for a factor. If you want to fill by two factors, you could try using interaction()
:
ggplot(plot_df[plot_df$area_code == area, ]) +
geom_col(aes(week, cpue, fill = interaction(activity, thres)), position = "dodge")