Reputation: 1433
I want the colour in this barplot
to be automatically filled with the minimum figure in RMSE column to be
greenthe nest to the minimum
RMSEfigures to be
yellowgreenthe middle figure to be
yellow, the second to the maximum RMSE to be
orangewhile the maximum figure in the RMSE column to be
red`.
df1 <- data.frame(Methods = c('b', 'a', 'c', 'd', 'e'), lb = c(9, 7, 9, 4, 9), RMSE = c(0.26177952, 0.11294586, 0.02452239, 0.08290467, 0.41488542))
ggplot2::ggplot(df1, ggplot2::aes(Methods, lb, fill = RMSE)) + # Using default colors
ggplot2::geom_bar(stat = "identity")# + col = rainbow(5)
I saw this online an I am imagining how it can hep me.
colfunc<-colorRampPalette(c("green", "yellowgreen", "yellow", "orange", "red"))
plot(rep(1,5),col=(colfunc(5)), pch=19,cex=2)
Upvotes: 0
Views: 47
Reputation: 66970
Here's one option using a palette ("Spectral" from the ColorBrewer schemes built into ggplot2) that coincidentally includes a spectrum from green-yellow-orange-red. (It also includes blue, which I avoided using the values = c(-0.15,1)
part -- that effectively says the numbers have to 15% lower than the actual range to be assigned blue, so it doesn't start using colors until the green range.
ggplot2::ggplot(df1, ggplot2::aes(Methods, lb, fill = RMSE)) + # Using default colors
ggplot2::geom_bar(stat = "identity") +
ggplot2::scale_fill_distiller(palette = "Spectral", values = c(-0.15,1)))
If you want to color based on the ranking of RMSE rather than the value of RMSE, you could turn it into a factor and use the five colors by name:
ggplot2::ggplot(df1, ggplot2::aes(Methods, lb, fill = as.factor(RMSE))) + # Using default colors
ggplot2::geom_bar(stat = "identity") +
ggplot2::scale_fill_manual(values = c("green", "yellowgreen", "yellow", "orange", "red"))
(Aesthetically you might want to pick other colors by their hex values, like maybe "#70B070" instead of "green" if you prefer certain specific colors.)
Upvotes: 1