【问题标题】:How to color-code the positive and negative bars in barplot using ggplot如何使用ggplot对barplot中的正负条进行颜色编码
【发布时间】:2018-01-26 14:20:58
【问题描述】:

我有以下数据:

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

我正在绘制条形图,如下所示:

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")

我得到以下情节:

我想将负数条着色为红色,将所有正数条着色为灰色。

【问题讨论】:

  • geom_bar(aes(fill = repo < 0), stat = "identity") + scale_fill_manual(guide = FALSE, breaks = c(TRUE, FALSE), values=c("gray", "red"))
  • Fantastic.Worked 喜欢魅力!

标签: r ggplot2 bar-chart


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 2019-11-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    相关资源
    最近更新 更多