簡體   English   中英

如何使用 ggplot 對 barplot 中的正條和負條進行顏色編碼

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

我有以下數據:

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

我得到以下情節: ggplot 條形圖

我想將負條着色為紅色,將所有正條着色為灰色。

編寫 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