ggplot2:如何将geom_text带到栏的前面?

时间:2018-07-18 11:29:47

标签: r ggplot2 graph geom-text

我想使用geom_text()我的图表,但是它显示在我的条形图的后面。

如何将其显示在栏的前面,以及如何将总金额显示为geom_text()

您会在我的数据集下方找到(编辑)

 BRNCH_NAME PRODUCTS FINAL_SCORE
1   A ANCH  BILL    1998
2   B ANCH  BILL    1216
7   G ANCH  BILL    1220
8   H ANCH  BILL    1217
16  A ANCH  TICKET  1998
17  B ANCH  TICKET  2331
18  C ANCH  TICKET  0
19  D ANCH  TICKET  0
20  E ANCH  TICKET  0
21  F ANCH  TICKET  0
22  G ANCH  TICKET  1665
31  A ANCH  SHOP    2331
32  B ANCH  SHOP    1154
33  C ANCH  SHOP    1165
34  D ANCH  SHOP    1388
35  E ANCH  SHOP    1265

my shown graph

ggplot(data = FINAL_SCORE_BAR,
       aes(x = reorder(FINAL_SCORE_BAR$BRNCH_NAME, FINAL_SCORE_BAR$FINAL_SCORE),
           y = FINAL_SCORE, fill = PRODUCTS, label = FINAL_SCORE)) +
    geom_text(size = 3, position = position_stack(vjust = 0.5))+
    labs(y = "HGA ALL FINAL SCORE", x = "ELEMENTS", face = "bold",
         colour = "black", csize = 5) +
    coord_flip() +
    geom_bar(stat = "identity", position = "stack")+
    theme(axis.text.x = element_text(size = 12, face = "bold"),
          axis.text.y = element_text(size = 12, face = "bold")) +
    scale_fill_brewer(palette = "Blues")

1 个答案:

答案 0 :(得分:0)

更改几何的顺序,例如:

ggplot(data = FINAL_SCORE_BAR,
       aes(x = reorder(FINAL_SCORE_BAR$BRNCH_NAME, FINAL_SCORE_BAR$FINAL_SCORE),
           y = FINAL_SCORE, fill = PRODUCTS, label = FINAL_SCORE)) +
    labs(y = "HGA ALL FINAL SCORE", x = "ELEMENTS", face = "bold",
         colour = "black", csize = 5) +
    coord_flip() +
    geom_bar(stat = "identity", position = "stack") +
    theme(axis.text.x = element_text(size = 12, face = "bold"),
          axis.text.y = element_text(size = 12, face = "bold")) +
    scale_fill_brewer(palette = "Blues") +
    # the order of geoms matters
    geom_text(size = 3, position = position_stack(vjust = 0.5))

更新是基于问题中的更新信息。


您的数据中缺少“ PRODUCTS”列。没有它,我的答案是这样的(如果没有软件包,则应安装data.tabletidyverse软件包:

library(tidyverse)

FINAL_SCORE_BAR <- 
    data.table::fread(stringsAsFactors = TRUE,
'BRNCH_NAME ELEMENTS FINAL_SCORE
"A ANCH"  BILL    1998
"B ANCH"  BILL    1216
"G ANCH"  BILL    1220
"H ANCH"  BILL    1217
"A ANCH"  TICKET  1998
"B ANCH"  TICKET  2331
"C ANCH"  TICKET  0
"D ANCH"  TICKET  0
"E ANCH"  TICKET  0
"F ANCH"  TICKET  0
"G ANCH"  TICKET  1665
"A ANCH"  SHOP    2331
"B ANCH"  SHOP    1154
"C ANCH"  SHOP    1165
"D ANCH"  SHOP    1388
"E ANCH"  SHOP    1265')


FINAL_SCORE_BAR <- FINAL_SCORE_BAR %>% 
    mutate(BRNCH_NAME = reorder(BRNCH_NAME, FINAL_SCORE, FUN = mean))

my_sum <- FINAL_SCORE_BAR %>% 
    group_by(BRNCH_NAME) %>% 
    summarize(SUM_FINAL_SCORE = sum(FINAL_SCORE))

ggplot(data = FINAL_SCORE_BAR,
       aes(x = BRNCH_NAME, y = FINAL_SCORE)) +
    labs(y = "HGA ALL FINAL SCORE", x = "ELEMENTS", face = "bold",
         colour = "black", csize = 5) +
    coord_flip(ylim = c(0, 7500)) +
    geom_bar(stat = "identity", position = "stack")+
    theme(axis.text.x = element_text(size = 12, face = "bold"),
          axis.text.y = element_text(size = 12, face = "bold")) +
    scale_fill_brewer(palette = "Blues") +
    # The sums:
    geom_text(data = my_sum,
              aes(x = BRNCH_NAME,
                y = SUM_FINAL_SCORE,
                label = SUM_FINAL_SCORE),
              size = 3,
              hjust = -0.2,
              inherit.aes = FALSE
    )