0

I want to use geom_text() my graph but it is shown behind my bargraphs.

How can I bring it in front of the bars and how to show the total amount as geom_text() also?

you will find below my data set(edit)

 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")
hamza
  • 3
  • 4
  • Those are two different questions: 1. *how can I bring it in front of bars* - just move your `geom_text` code after `geom_bar`; 2. I can't understand what you want – pogibas Jul 18 '18 at 11:33
  • Give us the example of you dataset if you want us to help you. Have you read [this questions](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and the answers on how to ask questions efficiently? – GegznaV Jul 18 '18 at 12:01

1 Answers1

1

Change the order of your geoms, e.g.:

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

UPDATE based on the updated information in the question.


Column "PRODUCTS" is missing in your data. Without it my answer is this (you should instal data.table and tidyverse packages if you don't have them:

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
    )

GegznaV
  • 4,938
  • 4
  • 23
  • 43
  • Thank you so much this is work :) ı also add my dataset, how to show their total amount at the end of each bar . – hamza Jul 18 '18 at 12:12
  • I cant see where your data is. Are you sure you read [this answer](https://stackoverflow.com/a/5963610/4783029) *carefully*? – GegznaV Jul 18 '18 at 12:23
  • By the way, if you think, that the answer was helpful, consider upvoting it. If it is the answer you were looking for -- accept it. – GegznaV Jul 18 '18 at 12:27
  • I have already accept your answer furthermore i have two questions in my ticket now i try to show total amount of each line at the graph also. – hamza Jul 18 '18 at 12:33
  • Column "PRODUCTS" is missing in your data. – GegznaV Jul 18 '18 at 13:03
  • Thank you for your answer as an update elements are products.Now i will update my post for further answers. – hamza Jul 18 '18 at 13:27
  • By the way scale_fill_brewer(palette = "Blues") code is not working at your second update. – hamza Jul 18 '18 at 14:51
  • Because there is no fill in the example. – GegznaV Jul 18 '18 at 14:55