Давайте сначала попробуем вывести ваши данные, поскольку вы не указали это в своем вопросе:
library(ggplot2)
set.seed(69)
x <- data.frame(x = rnorm(5000, 50, 10))
ggplot(data = x,
aes(x = x)) +
geom_histogram(fill = "navy",
alpha = 0.5) +
theme_minimal() +
geom_vline(xintercept = seq(30, 70, by = 10),
linetype = "dashed")
That's close enough for demonstration purposes.
It would be very difficult (though not impossible) to keep the plot the shape it is and color differently between the lines. It would also make little sense to have different colors within bins. However, it is fairly easy to do (and makes more sense) if you line up the bins with the vertical lines. Effectively we just manually bin and tabulate the data and plot it however we like:
df <- setNames(as.data.frame(table(1.25 + 2.5 * floor(x$x/2.5))), c("bin", "count"))
ggplot(df, aes(x = as.numeric(as.character(bin)), y = count, fill = bin)) +
geom_col(width = 2.5) +
theme_minimal() +
geom_vline(xintercept = seq(30, 70, by = 10),
linetype = "dashed") +
scale_fill_manual(values = rep(c(rep("gray75", 3),
rep(c("yellow", "gold", "orange", "red"), each = 2),
rep("gray75", 4)), each = 2),
guide = guide_none()) +
labs(x = "x")
Created on 2020-08-06 by the пакет REPEX (v0.3.0)