Home > Software engineering >  Adding total count of observations to barplot
Adding total count of observations to barplot

Time:01-26

I am currently working with survey data with 250 columns. A sample of my data looks like this:

q1 <- factor(c("yes","yes","no","yes",NA,"yes","no","yes"))
q2 <- factor(c("Albania","USA","Albania","Albania","UK",NA,"UK","Albania"))
q3 <- factor(c(0,1,0,0,1,1,0,0))
q4 <- factor(c(0,NA,NA,NA,1,NA,0,0))
q5 <- factor(c("Dont know","Prefer not to answer","Agree","Disagree",NA,"Agree","Agree",NA))
q6 <- factor(c(1,NA,3,5,800,NA,900,2))

data <- data.frame(q1,q2,q3,q4,q5,q6)

In order to loop through all columns and create list of barplots showing distribution of answers, I used code below:

barplot_list <- lapply(names(data), function(variable) {
  ggplot(
    data = data,
    mapping = aes(.data[[variable]])
  )  
    geom_bar(width = 0.6, fill = "#0096FF")  
    labs(x = variable, y = "response count")  
    geom_text(aes(label=..count..),stat='count',position=position_dodge(0.9),hjust=-0.1) 
    scale_fill_brewer(palette = "Set2")  
    theme_bw()  
    theme(panel.grid.major.y = element_blank())  
    coord_flip()
})

Now I want to add total count of observations to each barplot. Total count can be shown in legend or inside chart or in caption. I tried adding following line of code: geom_text(stat = "count",aes(label = after_stat(sum(count))))

But this shows total count on top of each bar. Is there a way to modify my code so that "Total = N" (N standing for total observations for each column(excluding NAs) in dataset) is shown somewhere in the plot? Thank you very much beforehand!

CodePudding user response:

the number of observations is going to be the same for every plot, because it's the number of rows of the dataframe... (8 in your example)

can add it in the caption like this:

  labs(caption = paste("Total =", nrow(data)))
  • Related