Search code examples
rggplot2geom-bar

Geom Bar Plot, sum of count not visible


I have a dataframe tag, with 51X5 structure

structure(list(Tagging = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("CIRCLE CAMPIAGN", 
    "NATIONAL CAMPIAGN"), class = "factor"), Status = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("Negative", "Positive"), class = "factor"), 
        Month = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
        3L, 3L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
        3L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 
        1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L), .Label = c("JUL", 
        "JUN", "MAY"), class = "factor"), Category = structure(c(1L, 
        4L, 6L, 1L, 2L, 4L, 6L, 1L, 2L, 4L, 5L, 6L, 1L, 2L, 4L, 5L, 
        6L, 1L, 2L, 4L, 5L, 6L, 1L, 2L, 4L, 6L, 1L, 4L, 6L, 2L, 3L, 
        4L, 6L, 1L, 2L, 3L, 4L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 
        3L, 4L, 5L, 6L, 6L), .Label = c("Data", "Other", "Roaming", 
        "Unlimited", "VAS", "Voice"), class = "factor"), count = c(3L, 
        2L, 1L, 4L, 5L, 2L, 1L, 2L, 6L, 7L, 2L, 3L, 4L, 9L, 6L, 2L, 
        3L, 3L, 3L, 10L, 2L, 5L, 5L, 5L, 4L, 3L, 1L, 1L, 1L, 2L, 
        1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 4L, 1L, 1L, 3L, 3L, 2L, 
        1L, 1L, 1L, 3L, 4L, 2L)), class = "data.frame", row.names = c(NA, 
    -51L))

I want to create a bar plot (ggplot) to show bar graph with label on bar as sum of count of category month wise I am using below code

ggplot(data = tag, aes(x = Tagging, y = count, fill = Status)) +
  geom_col() +
  labs(x = "Tagging", y = "Count", title = "FlyTxt ROI", subtitle = "Statistics") +
  geom_text(aes(label = count), color = "white", size = 3, position = position_stack(vjust = 0.5)) + 
  theme_minimal()+facet_wrap(~Month)

But I am getting split count values:

Help as I want only sum of count for each status


Solution

  • The problem is, that the information you show in the bar is accumulated by geom_col over all categories but the geom_text doesn't do that. On option is to pre-summarize the data (to get rid of the category split) and then plot the graph.

    library(tidyverse)
    
    tag_sum <- tag %>% 
      group_by(Tagging, Status, Month) %>% 
      summarise(count_sm = sum(count))
    
    ggplot(data = tag_sum, aes(x = Tagging, y = count_sm, fill = Status)) +
      geom_col() +
      geom_text(aes(label = count_sm), color = "white", size = 3, 
                position = position_stack(vjust = 0.5)) + 
      facet_wrap(~Month) + 
      labs(x = "Tagging", y = "Count", title = "FlyTxt ROI", subtitle = "Statistics") +
      theme_minimal() 
    

    enter image description here