Search code examples
rggplot2dplyrfacet

Show number of observation in each facet group ggplot2


I have this ggplot code and i think so far it went well according to my expectation.

    bi_tr%>%
  filter(!is.na(bi_tr$`13e Toilet type`)) %>%

  ggplot(aes(x=`12 Income`,fill=`13e Toilet type`,na.rm = TRUE))+ #this fill comment goes to define legend
  geom_histogram(binwidth=50,color="black")+ #setting default color for aes in histogram
  facet_wrap(vars(fill=`13e Toilet type`),nrow=3)+ #make 1 column and 3 rows
  labs(x="Income (USD/month)",y="Frequency",title = "Income by Toilet Type")+ #make title of axis and title
  theme(legend.title=element_blank(),strip.text.x = element_blank())+ #strip text.x deletes the title in each group
  scale_fill_manual(values =c("blue","yellow","grey"))+#set fill color
  theme(legend.justification=c(1,.5), legend.position=c(1,.5))   #setting legend location

But I want to show my total data observation in each facet group, I have tried stat_summary with fun.data=give.n yet the result showed "object 'give.n' not found"

Please help some newbie here... thanks a million


Solution

  • Without a reproducible dataset, it is difficult to be sure of the exact solution to your question, but you can try to add the count in a geom_text.

    Here an example to illustrate this:

    df <- data.frame(Income = sample(0:1000,900, replace = TRUE),
                     Type = rep(LETTERS[1:3], each = 300))
    
    library(dplyr)
    library(ggplot2)
    
    df %>% 
      filter(!is.na(Type)) %>%
      ggplot(aes(x = Income, fill = Type))+
      geom_histogram(binwidth = 50, color = "black")+
      facet_wrap(~Type, nrow = 3)+
      labs(x="Income (USD/month)",y="Frequency",title = "Income by Toilet Type")+ #make title of axis and title
      theme(legend.title=element_blank(),
            strip.text.x = element_blank())+ #strip text.x deletes the title in each group
      scale_fill_manual(values =c("blue","yellow","grey"))+
      theme(legend.justification=c(1,.5), legend.position=c(1,.5))+   #setting legend location
      geom_text(data = df%>% filter(!is.na(Type)) %>% count(Type), 
                aes(label = paste("Count:",n), y = Inf, x  = -Inf), vjust = 1, hjust = 0)
    

    enter image description here

    So, adapted to your code, it should look like something like this:

    bi_tr%>%
      filter(!is.na(`13e Toilet type`)) %>%  
      ggplot(aes(x=`12 Income`,fill=`13e Toilet type`,na.rm = TRUE))+ #this fill comment goes to define legend
      geom_histogram(binwidth=50,color="black")+ #setting default color for aes in histogram
      facet_wrap(~`13e Toilet type`,nrow=3)+ #make 1 column and 3 rows
      labs(x="Income (USD/month)",y="Frequency",title = "Income by Toilet Type")+ #make title of axis and title
      theme(legend.title=element_blank(),strip.text.x = element_blank())+ #strip text.x deletes the title in each group
      scale_fill_manual(values =c("blue","yellow","grey"))+#set fill color
      theme(legend.justification=c(1,.5), legend.position=c(1,.5))+   #setting legend location
      geom_text(data = bi_tr%>% filter(!is.na(`13e Toilet type`)) %>% count(`13e Toilet type`), 
                aes(label = paste("Count:",n), y = Inf, x  = -Inf), vjust = 1, hjust = 0)
    

    Does it answer your question ?

    If not, please provide a reproducible example of your dataset bi_tr by following guidelines provided in this post: How to make a great R reproducible example