简体   繁体   中英

How to color-code the positive and negative bars in barplot using ggplot

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: ggplot 条形图

I would like to color the negative bars as red and all positive bars as grey.

编写 Andrey Kolyadin 评论作为一种解决方案,以使其不再显示为未回答的问题:

geom_bar(aes(fill = repo < 0), stat = "identity") + scale_fill_manual(guide = FALSE, breaks = c(TRUE, FALSE), values=c("gray", "red"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM