Percentage labels after facet wrap

I have data here with class attendance. I have done facet wrap for 3 levels. I now want to put labels on the bars in the percentage format. How can I do this?

library(tidyverse)

nithin<-tibble::tribble(
  ~age, ~class_attendance,
    6L,             "Yes",
    6L,              "No",
    6L,             "Yes",
    7L,             "Yes",
    7L,             "Yes",
    7L,              "No",
    8L,              "No",
    8L,              "No",
    8L,             "Yes",
    6L,             "Yes",
    6L,              "No",
    8L,              "No",
    7L,              "No",
    6L,             "Yes",
    7L,              "No",
    8L,             "Yes",
    6L,             "Yes",
    8L,              "No"
  )
nithin %>% 
  mutate(level=if_else(age==6,"Level 1-Age 6",if_else(age==7,"Level 2-Age 7","Level 3- Age 8"))) %>% 
  drop_na(class_attendance) %>% 
  ggplot(aes(class_attendance))+
  geom_bar(width=0.5,mapping=aes(fill=class_attendance))+
  facet_wrap(~level)+
  theme_minimal()


Created on 2022-03-18 by the reprex package (v2.0.1)

Hello,

Here is a way of doing this by first calculating the percentages before you use them in the plots

library(tidyverse)

nithin<-tibble::tribble(
  ~age, ~class_attendance,
  6L,             "Yes",
  6L,              "No",
  6L,             "Yes",
  7L,             "Yes",
  7L,             "Yes",
  7L,              "No",
  8L,              "No",
  8L,              "No",
  8L,             "Yes",
  6L,             "Yes",
  6L,              "No",
  8L,              "No",
  7L,              "No",
  6L,             "Yes",
  7L,              "No",
  8L,             "Yes",
  6L,             "Yes",
  8L,              "No"
)

nithin %>% 
  mutate(level=if_else(age==6,"Level 1-Age 6",
                       if_else(age==7,"Level 2-Age 7","Level 3- Age 8"))) %>%
  drop_na(class_attendance) %>% 
  group_by(age) %>% 
  mutate(perc = sum(class_attendance == "Yes") / n(),
         perc = ifelse(class_attendance != "Yes", 1-perc, perc)) %>% 
  ungroup() %>% 
  
  ggplot(aes(class_attendance))+
  geom_bar(width=0.5,mapping=aes(fill=class_attendance))+
  facet_wrap(~level)+
  geom_text(aes(label = paste0(perc %>% round(2) * 100, "%")),
            stat = "count", vjust = -0.5)+
  theme_minimal()

Created on 2022-03-21 by the reprex package (v2.0.1)

Hope this helps,
PJ

The labelling has a problem. The graph of level-1-Age 6 shows a total of 140% (100+40).

Hi,

I'm sorry I did not realise you wanted the percentages by group. I edited my post with the new answer.

PJ

please note that GGally has stat_prop with custom grouping as you see fit

library(tidyverse)
library(GGally)
#> Registered S3 method overwritten by 'GGally':
#>   method from   
#>   +.gg   ggplot2
nithin<-tibble::tribble(
  ~age, ~class_attendance,
  6L,             "Yes",
  6L,              "No",
  6L,             "Yes",
  7L,             "Yes",
  7L,             "Yes",
  7L,              "No",
  8L,              "No",
  8L,              "No",
  8L,             "Yes",
  6L,             "Yes",
  6L,              "No",
  8L,              "No",
  7L,              "No",
  6L,             "Yes",
  7L,              "No",
  8L,             "Yes",
  6L,             "Yes",
  8L,              "No"
)

nithin <- nithin %>% 
  mutate(level=if_else(age==6,"Level 1-Age 6",
                       if_else(age==7,"Level 2-Age 7","Level 3- Age 8"))) %>%
  drop_na(class_attendance) 


  
  ggplot(nithin, aes(x= class_attendance, fill=class_attendance, by = 1))+
  geom_bar(width=0.5)+
  geom_text(
  aes(label = scales::percent(after_stat(prop), accuracy = 1)),
  stat = "prop",
  position = position_nudge(y=0.2))+
  facet_wrap(~level)+
  theme_minimal()

Created on 2022-03-21 by the reprex package (v2.0.1)

1 Like

Thanks a lot for this..

Regards,
NP

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.