Reputation: 53
I have this numeric variable 1:5, in my dataset D. I want to make a barplot, using the geom_bar function in ggplot, and change the colors of the bars, så each of the five bars have a different color.
I've tried to use the scale_fill_manual,
but that does not seem to work, and I've also tried to use fill inside my aes which neither works. I seems like the bars continues to be grey, no matter what I try.
My basic code to make the plot are:
ggplot(D, aes(x = I_imp)) +
scale_x_continuous(breaks = c(1, 2, 3, 4, 5),
labels = c("No Importance",
"Very Low",
"Somewhat Low",
"Somewhat High",
"Very High")) + geom_bar()
I want each of the 5 bars to be in different color. But how??
Thank you!
Upvotes: 2
Views: 759
Reputation: 1177
Add fill to your aesthetics:
ggplot(D, aes(x = I_imp, fill = I_imp))
This will change the filling color of the bars according to the variable, if you then want to manually determine the colors you can add + scale_fill_manual()
.
Upvotes: 1
Reputation: 79
You can use the fill argument in geom_bar(), I use hex colors, you may want to google that if you are not familiar.
library(ggplot2)
D = data.frame(1:5)
colnames(D) <- "I_imp"
ggplot(D, aes(x = I_imp)) +
scale_x_continuous(breaks = c(1, 2, 3, 4, 5),
labels = c("No Importance",
"Very Low",
"Somewhat Low",
"Somewhat High",
"Very High")) +
geom_bar(fill = c("#a83232", "#512fad", "#a8a82c", "#c21b9e", "#32a852"))
Upvotes: 0