I have a pretty basic bar chart but I want to have the colours be conditional on the value. i.e. between 17-20 it's green, 14-16 it's yellow etc. I believe that ggplot assigns colours in order, so they won't always match up as I'm producing several reports for different department. Some sample code:
factor <- c ("Factor1", "Factor2", "Factor3", "Factor4", "Factor5")
mean <- c (9, 18, 14, 19, 17)
df <- data.frame (factor, mean)
#Add cutpoints
df <- df %>%
mutate (factor = as.factor(factor),
concern = cut (mean,
breaks = c(-Inf, 9, 13, 17, Inf),
labels = c("Serious", "Significant", "Minimal", "Strengths"),
right = TRUE)
)
#COlour Palette
pal <- c("forestgreen", "yellow", "orange", "red")
df %>%
ggplot (aes (x = factor, y = mean, fill = concern)) +
geom_col() +
coord_flip() +
scale_x_discrete(limits = rev(levels(df$factor))) +
scale_fill_manual(values = pal)
Produces this:
I want Factor1 to be red, Factor2 and 4 to be green, etc. I also need it to assign colours based on the column "mean" as the next chart will have different results.
Hope this makes sense