In ggplot2/facet_grid, how to change column a and b fill in red in facet gap ?
library(tidyverse)
data <- data.frame(category=c('a','b','c','d'),actual=c(10,7,6,3),budget =c(11,12,4,1))
data %>% mutate(gap = actual - budget) %>% gather(key='item',value='amount',-category) %>%
ggplot(aes(x=category,y=amount,fill=item))+
geom_col()+
scale_fill_brewer(palette = "Spectral",direction=1)+
facet_wrap(~ item,ncol=1)
I try to replace geom_col() to geom_col(aes(fill = if_else(amount<0 & item=='gap','red','lightblue'))) , it's failed
data %>% mutate(gap = actual - budget) %>% gather(key='item',value='amount',-category) %>%
ggplot(aes(x=category,y=amount,fill=item))+
geom_col(aes(fill = if_else(amount<0 & item=='gap','red','lightblue')))+
scale_fill_brewer(palette = "Spectral",direction=1)+
facet_wrap(~ item,ncol=1)


One way would be to use a nested
ifelseandscale_fill_manual: