Placing total counts above stacked bars of barplot where stacked bars are already labeled with percentages...












0
















This question already has an answer here:




  • How to put labels over geom_bar in R with ggplot2

    2 answers




This is my data frame:



no_q1 <- data.frame(stringsAsFactors=FALSE,
count = c(47L, 36L, 51L, 44L, 34L, 38L, 44L, 33L, 22L, 27L, 16L,
17L, 13L, 2L, 7L, 11L, 10L, 13L, 8L, 3L),
prop = c(0.661971830985915, 0.553846153846154,
0.671052631578947, 0.619718309859155,
0.62962962962963, 0.575757575757576, 0.745762711864407,
0.464788732394366, 0.338461538461538, 0.355263157894737,
0.225352112676056, 0.314814814814815, 0.196969696969697,
0.0338983050847458, 0.0985915492957746, 0.169230769230769,
0.131578947368421, 0.183098591549296,
0.148148148148148, 0.0454545454545455),
sum = c("71", "65", "76", "71", "54", "66", "59", "71", "65",
"76", "71", "54", "66", "59", "71", "65", "76",
"71", "54", "66"),
Participant = as.factor(c("q5", "q5", "q5", "q5", "q5", "q5", "q5",
"q6", "q6", "q6", "q6", "q6", "q6", "q6",
"q8", "q8", "q8", "q8", "q8", "q8")),
Month = as.factor(c("Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Apr", "May", "Jun", "Jul",
"Aug", "Sep"))
)


and this is my plot:



ggplot(no_q1, aes(x = Month, y = prop, fill = Participant)) +
geom_bar(position = "stack", stat = "identity") +
geom_text(position = position_stack(vjust = 0.5), aes(label =
paste0(round(no_q1$prop*100, 2), "%"))) +
geom_text(aes(label = sum, y = 1.3))


My question is how can I make the counts appear exactly above the bars. Currently they all have the same height but I want them to be exactly above each bar. Thanks for any suggestions.










share|improve this question















marked as duplicate by Chris, MLavoie, Unheilig, Lajos Arpad, Bilal Ahmed Nov 22 '18 at 12:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Unfortunately, the answer in the link does not work for this problem

    – Pascal Schmidt
    Nov 22 '18 at 1:04
















0
















This question already has an answer here:




  • How to put labels over geom_bar in R with ggplot2

    2 answers




This is my data frame:



no_q1 <- data.frame(stringsAsFactors=FALSE,
count = c(47L, 36L, 51L, 44L, 34L, 38L, 44L, 33L, 22L, 27L, 16L,
17L, 13L, 2L, 7L, 11L, 10L, 13L, 8L, 3L),
prop = c(0.661971830985915, 0.553846153846154,
0.671052631578947, 0.619718309859155,
0.62962962962963, 0.575757575757576, 0.745762711864407,
0.464788732394366, 0.338461538461538, 0.355263157894737,
0.225352112676056, 0.314814814814815, 0.196969696969697,
0.0338983050847458, 0.0985915492957746, 0.169230769230769,
0.131578947368421, 0.183098591549296,
0.148148148148148, 0.0454545454545455),
sum = c("71", "65", "76", "71", "54", "66", "59", "71", "65",
"76", "71", "54", "66", "59", "71", "65", "76",
"71", "54", "66"),
Participant = as.factor(c("q5", "q5", "q5", "q5", "q5", "q5", "q5",
"q6", "q6", "q6", "q6", "q6", "q6", "q6",
"q8", "q8", "q8", "q8", "q8", "q8")),
Month = as.factor(c("Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Apr", "May", "Jun", "Jul",
"Aug", "Sep"))
)


and this is my plot:



ggplot(no_q1, aes(x = Month, y = prop, fill = Participant)) +
geom_bar(position = "stack", stat = "identity") +
geom_text(position = position_stack(vjust = 0.5), aes(label =
paste0(round(no_q1$prop*100, 2), "%"))) +
geom_text(aes(label = sum, y = 1.3))


My question is how can I make the counts appear exactly above the bars. Currently they all have the same height but I want them to be exactly above each bar. Thanks for any suggestions.










share|improve this question















marked as duplicate by Chris, MLavoie, Unheilig, Lajos Arpad, Bilal Ahmed Nov 22 '18 at 12:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Unfortunately, the answer in the link does not work for this problem

    – Pascal Schmidt
    Nov 22 '18 at 1:04














0












0








0


0







This question already has an answer here:




  • How to put labels over geom_bar in R with ggplot2

    2 answers




This is my data frame:



no_q1 <- data.frame(stringsAsFactors=FALSE,
count = c(47L, 36L, 51L, 44L, 34L, 38L, 44L, 33L, 22L, 27L, 16L,
17L, 13L, 2L, 7L, 11L, 10L, 13L, 8L, 3L),
prop = c(0.661971830985915, 0.553846153846154,
0.671052631578947, 0.619718309859155,
0.62962962962963, 0.575757575757576, 0.745762711864407,
0.464788732394366, 0.338461538461538, 0.355263157894737,
0.225352112676056, 0.314814814814815, 0.196969696969697,
0.0338983050847458, 0.0985915492957746, 0.169230769230769,
0.131578947368421, 0.183098591549296,
0.148148148148148, 0.0454545454545455),
sum = c("71", "65", "76", "71", "54", "66", "59", "71", "65",
"76", "71", "54", "66", "59", "71", "65", "76",
"71", "54", "66"),
Participant = as.factor(c("q5", "q5", "q5", "q5", "q5", "q5", "q5",
"q6", "q6", "q6", "q6", "q6", "q6", "q6",
"q8", "q8", "q8", "q8", "q8", "q8")),
Month = as.factor(c("Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Apr", "May", "Jun", "Jul",
"Aug", "Sep"))
)


and this is my plot:



ggplot(no_q1, aes(x = Month, y = prop, fill = Participant)) +
geom_bar(position = "stack", stat = "identity") +
geom_text(position = position_stack(vjust = 0.5), aes(label =
paste0(round(no_q1$prop*100, 2), "%"))) +
geom_text(aes(label = sum, y = 1.3))


My question is how can I make the counts appear exactly above the bars. Currently they all have the same height but I want them to be exactly above each bar. Thanks for any suggestions.










share|improve this question

















This question already has an answer here:




  • How to put labels over geom_bar in R with ggplot2

    2 answers




This is my data frame:



no_q1 <- data.frame(stringsAsFactors=FALSE,
count = c(47L, 36L, 51L, 44L, 34L, 38L, 44L, 33L, 22L, 27L, 16L,
17L, 13L, 2L, 7L, 11L, 10L, 13L, 8L, 3L),
prop = c(0.661971830985915, 0.553846153846154,
0.671052631578947, 0.619718309859155,
0.62962962962963, 0.575757575757576, 0.745762711864407,
0.464788732394366, 0.338461538461538, 0.355263157894737,
0.225352112676056, 0.314814814814815, 0.196969696969697,
0.0338983050847458, 0.0985915492957746, 0.169230769230769,
0.131578947368421, 0.183098591549296,
0.148148148148148, 0.0454545454545455),
sum = c("71", "65", "76", "71", "54", "66", "59", "71", "65",
"76", "71", "54", "66", "59", "71", "65", "76",
"71", "54", "66"),
Participant = as.factor(c("q5", "q5", "q5", "q5", "q5", "q5", "q5",
"q6", "q6", "q6", "q6", "q6", "q6", "q6",
"q8", "q8", "q8", "q8", "q8", "q8")),
Month = as.factor(c("Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Apr", "May", "Jun", "Jul",
"Aug", "Sep"))
)


and this is my plot:



ggplot(no_q1, aes(x = Month, y = prop, fill = Participant)) +
geom_bar(position = "stack", stat = "identity") +
geom_text(position = position_stack(vjust = 0.5), aes(label =
paste0(round(no_q1$prop*100, 2), "%"))) +
geom_text(aes(label = sum, y = 1.3))


My question is how can I make the counts appear exactly above the bars. Currently they all have the same height but I want them to be exactly above each bar. Thanks for any suggestions.





This question already has an answer here:




  • How to put labels over geom_bar in R with ggplot2

    2 answers








r ggplot2 geom-bar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 1:06







Pascal Schmidt

















asked Nov 22 '18 at 0:39









Pascal SchmidtPascal Schmidt

44




44




marked as duplicate by Chris, MLavoie, Unheilig, Lajos Arpad, Bilal Ahmed Nov 22 '18 at 12:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Chris, MLavoie, Unheilig, Lajos Arpad, Bilal Ahmed Nov 22 '18 at 12:25


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Unfortunately, the answer in the link does not work for this problem

    – Pascal Schmidt
    Nov 22 '18 at 1:04



















  • Unfortunately, the answer in the link does not work for this problem

    – Pascal Schmidt
    Nov 22 '18 at 1:04

















Unfortunately, the answer in the link does not work for this problem

– Pascal Schmidt
Nov 22 '18 at 1:04





Unfortunately, the answer in the link does not work for this problem

– Pascal Schmidt
Nov 22 '18 at 1:04












1 Answer
1






active

oldest

votes


















0














library(tidyverse)

# dataset to use to plot the sum number above stacked bars
d2 = no_q1 %>% group_by(Month, sum) %>% summarise(prop = sum(prop))

ggplot() +
geom_bar(data=no_q1, aes(x = Month, y = prop, fill = Participant), position = "stack", stat = "identity") +
geom_text(data=no_q1, position = position_stack(vjust = 0.5), aes(x = Month, y = prop, group = Participant, label = paste0(round(no_q1$prop*100, 2), "%"))) +
geom_text(data=d2, aes(x = Month, y = prop, label=sum),vjust=0)


enter image description here






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    library(tidyverse)

    # dataset to use to plot the sum number above stacked bars
    d2 = no_q1 %>% group_by(Month, sum) %>% summarise(prop = sum(prop))

    ggplot() +
    geom_bar(data=no_q1, aes(x = Month, y = prop, fill = Participant), position = "stack", stat = "identity") +
    geom_text(data=no_q1, position = position_stack(vjust = 0.5), aes(x = Month, y = prop, group = Participant, label = paste0(round(no_q1$prop*100, 2), "%"))) +
    geom_text(data=d2, aes(x = Month, y = prop, label=sum),vjust=0)


    enter image description here






    share|improve this answer




























      0














      library(tidyverse)

      # dataset to use to plot the sum number above stacked bars
      d2 = no_q1 %>% group_by(Month, sum) %>% summarise(prop = sum(prop))

      ggplot() +
      geom_bar(data=no_q1, aes(x = Month, y = prop, fill = Participant), position = "stack", stat = "identity") +
      geom_text(data=no_q1, position = position_stack(vjust = 0.5), aes(x = Month, y = prop, group = Participant, label = paste0(round(no_q1$prop*100, 2), "%"))) +
      geom_text(data=d2, aes(x = Month, y = prop, label=sum),vjust=0)


      enter image description here






      share|improve this answer


























        0












        0








        0







        library(tidyverse)

        # dataset to use to plot the sum number above stacked bars
        d2 = no_q1 %>% group_by(Month, sum) %>% summarise(prop = sum(prop))

        ggplot() +
        geom_bar(data=no_q1, aes(x = Month, y = prop, fill = Participant), position = "stack", stat = "identity") +
        geom_text(data=no_q1, position = position_stack(vjust = 0.5), aes(x = Month, y = prop, group = Participant, label = paste0(round(no_q1$prop*100, 2), "%"))) +
        geom_text(data=d2, aes(x = Month, y = prop, label=sum),vjust=0)


        enter image description here






        share|improve this answer













        library(tidyverse)

        # dataset to use to plot the sum number above stacked bars
        d2 = no_q1 %>% group_by(Month, sum) %>% summarise(prop = sum(prop))

        ggplot() +
        geom_bar(data=no_q1, aes(x = Month, y = prop, fill = Participant), position = "stack", stat = "identity") +
        geom_text(data=no_q1, position = position_stack(vjust = 0.5), aes(x = Month, y = prop, group = Participant, label = paste0(round(no_q1$prop*100, 2), "%"))) +
        geom_text(data=d2, aes(x = Month, y = prop, label=sum),vjust=0)


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 1:05









        AntoniosKAntoniosK

        12.6k1922




        12.6k1922

















            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith