I have the following data:
df
rowname repo
1 revrepo 0.888
2 bankrate 0.402
3 CRR 0.250
4 Callrate 0.723
5 WPI 0.049
6 GDP -0.318
7 FED 0.110
8 width 0.209
9 nse 0.059
10 usd 0.185
I am plotting the barplot as shown below:
df %>% mutate(rowname = factor(rowname, levels = rowname[order(repo)])) %>%
ggplot(aes(x = rowname, y = repo)) +
geom_bar(stat = "identity") +
ylab("Correlation with repo") +
xlab("Independent Variable")
I get the following plot:
I would like to color the negative bars as red and all positive bars as grey.
1. If you have Excel 2013, choose the Format Data Series from the right click menu to open the Format Data Series pane, and then click Fill & Line icon, and check Invert if negative option, then check Solid fill and specify the colors for the positive and negative data bar as you want beside Color section.
To set colors for bars in Bar Plot drawn using barplot() function, pass the required color value(s) for col parameter in the function call. col parameter can accept a single value for color, or a vector of color values to set color(s) for bars in the bar plot.
You can change the color of bars in a barplot using color argument. RGB is a way of making colors. You have to to provide an amount of red, green, blue, and the transparency value to the color argument and it returns a color.
The color attribute is used to set the color of the bars(maroon in this case).
Writing Andrey Kolyadin comment as a solution to make it stop it appearing as a question unanswered:
geom_bar(aes(fill = repo < 0), stat = "identity") + scale_fill_manual(guide = FALSE, breaks = c(TRUE, FALSE), values=c("gray", "red"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With