R plot color by value in y-axis ggplot

No need for ifelse statement, just write your condition and then use scale_fill_manual:

ggplot(Report, aes(x=Report$Name,y=Report$average_working_hours)) + 
  ggtitle('working hours in July') + 
  ylab(' working hours') + 
  geom_bar(stat = 'identity', aes(fill = Report$average_working_hours > 8)) + 
  theme_gray() + 
  scale_fill_manual(values=c('blue', 'red'))

This can be done quite simply using dplyr.

First thing I would do is create a column, “Working hours > 8” then use that value is the fill in ggplot as follows:

Report %>% 
 mutate(larger_than_8 = ifelse(Report$average_working_hours > 8, 1, 0)) %>%
 ggplot(., aes(x = Name, y = average_working_hours, fill = larger_than_8)) +
 geom_bar(stat = 'identity') + 
 theme_gray() + 
 scale_fill_manual(values=c('blue', 'red')) + 
 ggtitle('working hours in July') + 
 ylab('working hours')

You can change the binary classification in the ifelse statement to any text you’d like to show in the legend.

Read more here: Source link