简体   繁体   English

如何使用 ggplot 对 barplot 中的正条和负条进行颜色编码

[英]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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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