简体   繁体   English

R ggplot2条形图更改特定条形的颜色

[英]R ggplot2 bar chart change color of specific bars

I have this dataframe called earningsCont that I will use to make a stacked bar chart: 我有一个名为IncomeCont的数据框,将用于制作堆叠的条形图:

                       Total     Ghost
JPYUSD          -39837298.85         0
GBPUSD           -1981571.03 -39837299
EURUSD           -1251394.07 -41818870
MXNUSD            -498204.15 -43070264
NZDUSD            -333466.88 -43568468
PLNUSD            -215056.56 -43901935
CNYUSD            -143457.92 -44116992
AUDUSD            -130047.28 -44260449
CADUSD             -94396.57 -44390497
CHFUSD             -46730.39 -44484893
HKDUSD             -24084.79 -44531624
BRLUSD                  0.00 -44555709
Diversification  -4276539.06 -40279169
Portfolio       -40279169.44         0

I then melt it to look like this: 然后,我将其融化为以下形式:

              Var1  Var2        value
1           JPYUSD Total -39837298.85
2           GBPUSD Total  -1981571.03
3           EURUSD Total  -1251394.07
4           MXNUSD Total   -498204.15
5           NZDUSD Total   -333466.88
6           PLNUSD Total   -215056.56
7           CNYUSD Total   -143457.92
8           AUDUSD Total   -130047.28
9           CADUSD Total    -94396.57
10          CHFUSD Total    -46730.39
11          HKDUSD Total    -24084.79
12          BRLUSD Total         0.00
13 Diversification Total  -4276539.06
14       Portfolio Total -40279169.44
15          JPYUSD Ghost         0.00
16          GBPUSD Ghost -39837298.85
17          EURUSD Ghost -41818869.88
18          MXNUSD Ghost -43070263.95
19          NZDUSD Ghost -43568468.11
20          PLNUSD Ghost -43901934.98
21          CNYUSD Ghost -44116991.55
22          AUDUSD Ghost -44260449.46
23          CADUSD Ghost -44390496.75
24          CHFUSD Ghost -44484893.32
25          HKDUSD Ghost -44531623.72
26          BRLUSD Ghost -44555708.50
27 Diversification Ghost -40279169.44
28       Portfolio Ghost         0.00

I use this code to make the chart: 我使用以下代码制作图表:

ggplot(data=earningsCont, aes(x=Var1, y=value, fill=Var2, order=-as.numeric(Var2))) + geom_bar(stat="identity")+
    theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
    labs(x="", y="") + 
    scale_y_continuous(labels = comma) +
    ggtitle("Earnings at Risk:  Unhedged (USD)") +
    scale_fill_manual(values = c(wfGold, wfWhite))+ guides(fill=FALSE)

That makes it look like the attached picture. 这使它看起来像所附的图片。 As you can see, one of the sets of columns, "Ghost," is white so you can't see it. 如您所见,一组列之一“ Ghost”是白色的,因此您看不到它。 The other is all gold. 另一个全是金。 I just want to change the last two gold columns to colors that I have defined as wfGreen and wfRed. 我只想将最后两个金列更改为我定义为wfGreen和wfRed的颜色。 领悟

Also, why do I have to melt it before I turn it into a bar chart? 另外,为什么在将其变成条形图之前必须将其熔化? Is there now way to say series1=earningsCont$Total and series2=earningsCont$Ghost? 现在是否有办法说series1 = earningsCont $ Total和series2 = earningsCont $ Ghost?

You're coloring your bars based on the value of Var2 , which contains no information about whether the element is one of the last two columns ('Diversification' and 'Portfolio'). 您正在根据Var2的值对条进行着色,该值不包含有关元素是否为后两列之一(“ Diversification”和“ Portfolio”)的信息。 You need a grouping column in your dataframe which contains this information. 您需要在数据框中包含此信息的分组列。 You can add this manually, or using the following (clunky) code: 您可以手动添加此代码,也可以使用以下(笨拙的)代码添加此代码:

earningsConst.plot <- within(earningsConst, {
  Var3 <- ifelse(Var2 == "Ghost", "Ghost",
    ifelse(Var1 == "Diversification", "Div",
      ifelse(Var1 == "Portfolio", "Port",
        "Total")))
})

This should create a new dataframe identical to the last, but with a four-level coloring column Var3 to give to the fill aesthetic. 这将创建一个与上一个相同的新数据Var3 ,但具有四级着色列Var3以赋予fill美感。 You can tweak your original code to plot like this: 您可以调整原始代码以进行如下绘制:

ggplot(data=earningsCont.plot, aes(x=Var1, y=value, fill=Var3, order=-as.numeric(Var2))) + geom_bar(stat="identity")+
    theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
    labs(x="", y="") + 
    scale_y_continuous(labels = comma) +
    ggtitle("Earnings at Risk:  Unhedged (USD)") +
    scale_fill_manual(values = c(wfGold, wfRed, wfWhite, wfGreen))+ guides(fill=FALSE)

Note that as you didn't dput your data I was unable to test this code (I opted not to manually type in your whole dataframe). 请注意,由于您没有dput数据,因此无法测试此代码(我选择不手动键入整个数据框)。 While I'm farily certain that this should work, it's possible I've made a small mistake somewhere. 尽管我敢肯定这应该可行,但我可能在某个地方犯了一个小错误。 If you find one, let me know- or better yet dput(earningsConst) so as to have an easily-reproducible example. 如果找到一个,让我知道,或者更好的是dput(earningsConst) ,以便有一个容易复制的示例。

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

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