1

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

  • Please provide a reproducible example of your dataset by following this link: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Apr 29 '20 at 03:37
  • 1
    I think the special value you are looking for is `..count..`, but it will be very much easier to help with at least a sample of your data, as @dc37 says. – Ian Campbell Apr 29 '20 at 03:40

1 Answers1

1

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

dc37
  • 15,840
  • 4
  • 15
  • 32
  • BOOOM!!! Yeah that is all I want! Though I don't get all the meaning of that line. But, thanks a lot ! – Gugi Yogaswara Apr 29 '20 at 05:02
  • You're welcome ;). The line with pipe calculate count "on the fly" in order to feed the `aes` of `geom_text`. Glad that it help you to answer your question. – dc37 Apr 29 '20 at 05:42