我想在ggplot2的堆叠条形图中显示数据值

时间:2019-06-11 20:01:55

标签: r ggplot2

我已经成功创建了一个堆积的条形图,但是我不能添加表示百分比的标签。这就是我所缺少的。 我基本上不知道如何正确使用geom_label / geom_text,我尝试了许多解决方案,但没有任何效果。

enter image description here

我尝试了geom_text函数,但是它一直告诉我我做错了。

year Month2 Month Day HE Supply MUnit    MPrice MBlock Fuel
2017    1   Jan   01    8   9408    SD2  15.38  126   COAL
2017    1   Jan   01    9   9388    SD3  15.46  218   COAL
2017    1   Jan   01    10  9393    SD3  15.46  218   COAL
2017    1   Jan   01    11  9628    SD4  15.47  203   COAL
2017    1   Jan   01    12  9943    EGC1 21.40  72    GAS
2017    1   Jan   01    13  10106   BR5  21.41  245   COAL
2017    1   Jan   01    14  10114   BR5  21.41  245   COAL
2017    1   Jan   01    15  9971    EGC1 20.75  75    GAS
2017    1   Jan   01    16  10302   BR5  21.41  245   COAL
2017    1   Jan   01    17  10655   TC01 22.77  11    GAS
2017    1   Jan   01    18  10811   CAL1 24.88  25    GAS
2017    1   Jan   01    19  10821   CAL1 24.88  25    GAS
2017    1   Jan   01    20  10765   BIG  26.00  30    HYDRO
2017    1   Jan   02    8   10428   CAL1 22.04  30    GAS
2017    1   Jan   02    9   10723   CAL1 29.97  59    GAS
2017    1   Jan   02    10  10933   BRA  44.50  30    HYDRO
2017    1   Jan   02    11  11107   ANC1 46.46  63    GAS
2017    1   Jan   02    12  11098   ANC1 46.46  38    GAS
2017    1   Jan   02    13  10839   JOF1 26.59  45    GAS
2017    1   Jan   02    14  10814   JOF1 26.09  15    GAS
2017    1   Jan   02    15  10797   BIG  26.00  30    HYDRO

sp <- ggplot(data = MU17) +      
       geom_bar(mapping = aes(x = factor(Month,levels=month.abb),
                fill = factor(Fuel, levels=c("COAL", "GAS","HYDRO","BIOMASS"))),
                position = "Fill") +
       scale_y_continuous(labels = scales::percent) 


sp + scale_fill_manual(breaks=c("COAL", "GAS","HYDRO","BIOMASS"), 
                      values=c("black","yellow","blue","green")) + 
     labs(x = "2017" , y="Marginal Fuel Between HE8 & HE20") + 
     labs(fill="Fuel Type")

我希望得到与我得到的图完全相同的图,只是带有指示百分比的标签。

1 个答案:

答案 0 :(得分:0)

我个人更喜欢使用geom_col而不是geom_bar自己处理数据,而不是让ggplot2来处理。这样,您可以更好地控制发生的事情。

由于您尚未提供所有数据,因此仅使用您提供的代码段。

library(tibble)
MU17 <- tribble(~year, ~Month2, ~Month, ~Day, ~HE, ~Supply, ~MUnit, ~MPrice, ~MBlock, ~Fuel,
                    2017,    1,   "Jan",   01,    8,   9408,    "SD2",  15.38,  126,   "COAL",
                    2017,    1,   "Jan",   01,    9,   9388,    "SD3",  15.46,  218,  "COAL",
                    2017,    1,   "Jan",   01,    10,  9393,    "SD3",  15.46,  218,   "COAL",
                    2017,    1,   "Jan",   01,    11,  9628,    "SD4",  15.47,  203,   "COAL",
                    2017,    1,   "Jan",   01,    12,  9943,    "EGC1", 21.40,  72,    "GAS",
                    2017,    1,   "Jan",   01,    13,  10106,   "BR5",  21.41,  245,   "COAL",
                    2017,    1,   "Jan",   01,    14,  10114,   "BR5",  21.41,  245,   "COAL",
                    2017,    1,   "Jan",   01,    15,  9971,    "EGC1", 20.75,  75,    "GAS",
                    2017,    1,   "Jan",   01,    16,  10302,   "BR5",  21.41,  245,   "COAL",
                    2017,    1,   "Jan",   01,    17,  10655,   "TC01", 22.77,  11,    "GAS",
                    2017,    1,   "Jan",   01,    18,  10811,   "CAL1", 24.88,  25,    "GAS",
                    2017,    1,   "Jan",   01,    19,  10821,   "CAL1", 24.88,  25,    "GAS",
                    2017,    1,   "Jan",   01,    20,  10765,   "BIG",  26.00,  30,    "HYDRO",
                    2017,    1,   "Jan",   02,    8,   10428,   "CAL1", 22.04,  30,    "GAS",
                    2017,    1,   "Jan",   02,    9,   10723,   "CAL1", 29.97,  59,    "GAS",
                    2017,    1,   "Jan",   02,    10,  10933,   "BRA",  44.50,  30,    "HYDRO",
                    2017,    1,   "Jan",   02,    11,  11107,   "ANC1", 46.46,  63,    "GAS",
                    2017,    1,   "Jan",   02,    12,  11098,   "ANC1", 46.46,  38,    "GAS",
                    2017,    1,   "Jan",   02,    13,  10839,   "JOF1", 26.59,  45,    "GAS",
                    2017,    1,   "Jan",   02,    14,  10814,   "JOF1", 26.09,  15,    "HYDRO",
                    2017,    1,   "Jan",   02,    15,  10797,   "BIG",  26.00,  30,    "BIOMASS",

                    2017,    2,   "Feb",   01,    8,   9408,    "SD2",  15.38,  126,   "COAL",
                    2017,    2,   "Feb",   01,    9,   9388,    "SD3",  15.46,  218,  "COAL",
                    2017,    2,   "Feb",   01,    10,  9393,    "SD3",  15.46,  218,   "COAL",
                    2017,    2,   "Feb",   01,    11,  9628,    "SD4",  15.47,  203,   "COAL",
                    2017,    2,   "Feb",   01,    12,  9943,    "EGC1", 21.40,  72,    "GAS",
                    2017,    2,   "Feb",   01,    13,  10106,   "BR5",  21.41,  245,   "COAL",
                    2017,    2,   "Feb",   01,    14,  10114,   "BR5",  21.41,  245,   "COAL",
                    2017,    2,   "Feb",   01,    15,  9971,    "EGC1", 20.75,  75,    "GAS",
                    2017,    2,   "Feb",   01,    16,  10302,   "BR5",  21.41,  245,   "COAL",
                    2017,    2,   "Feb",   01,    17,  10655,   "TC01", 22.77,  11,    "GAS",
                    2017,    2,   "Feb",   01,    18,  10811,   "CAL1", 24.88,  25,    "GAS",
                    2017,    2,   "Feb",   01,    19,  10821,   "CAL1", 24.88,  25,    "GAS",
                    2017,    2,   "Feb",   01,    20,  10765,   "BIG",  26.00,  30,    "HYDRO",
                    2017,    2,   "Feb",   02,    8,   10428,   "CAL1", 22.04,  30,    "GAS",
                    2017,    2,   "Feb",   02,    9,   10723,   "CAL1", 29.97,  59,    "GAS",
                    2017,    2,   "Feb",   02,    10,  10933,   "BRA",  44.50,  30,    "HYDRO",
                    2017,    2,   "Feb",   02,    11,  11107,   "ANC1", 46.46,  63,    "GAS",
                    2017,    2,   "Feb",   02,    12,  11098,   "ANC1", 46.46,  38,    "GAS",
                    2017,    2,   "Feb",   02,    13,  10839,   "JOF1", 26.59,  45,    "GAS",
                    2017,    2,   "Feb",   02,    14,  10814,   "JOF1", 26.09,  15,    "HYDRO",
                    2017,    2,   "Feb",   02,    15,  10797,   "BIG",  26.00,  30,    "BIOMASS"
    )

进行处理时,我计算:

the number of occurences/observations  (n)
their relative frequency per month (p)
a percent label of p (p2)
the y-position in the bar chart of each label (pos)

我将此数据输送到ggplot中。重要的是我将geom_colposition = “fill”一起使用。由于我为pos提供了一个正值geom_text,因此有必要在此处使用position = “identity”。此外,您需要某种ifelse声明来将colour的{​​{1}}调整为白色geom_text,以使#FFFFFF和{{1} }。

使用这种方法对您的原始数据表示好运。

HYDRO