Search code examples
rggplot2geom-bar

How to access some specific classes in a column using r?


I am having problems with accessing specific classes in a column. I have the dataframe as below:

library(ggplot2)
library(dplyr)

dat <- data.frame(
  time = factor(c("Breakfast","Breakfast","Breakfast","Breakfast","Breakfast","Lunch","Lunch","Lunch","Lunch","Lunch","Lunch","Dinner","Dinner","Dinner","Dinner","Dinner","Dinner","Dinner"), levels=c("Breakfast","Lunch","Dinner")),
  class = c("a","a","b","b","c","a","b","b","c","c","c","a","a","b","b","b","c","c"))

In the column time I'm interested in detecting only Breakfast and Dinner for class a, b and c So from that dataframe i just want to view it in table and it will look like this:

            a    b    c
Breakfast   2    2    1
Dinner      2    3    2

so for each class a,b,c I want to draw two bars. For example class a one bar represent: The average of Breakfast compare to other classes: 2/(2+2+1) and one other bar represent Dinner compare to other classes : 2/(2+3+2) and set them to different colors. I want to same for class b and class c.

Any help for this would be much appreciated.


Solution

  • We can subset and table after dropping the levels with droplevels

    table(droplevels(subset(dat, time %in% c("Breakfast", "Dinner"))))
    #           class
    #time        a b c
    #  Breakfast 2 2 1
    # Dinner    2 3 2
    

    If we need a barplot

    barplot(prop.table(table(droplevels(subset(dat, time %in% 
                c("Breakfast", "Dinner")))), 1), beside = TRUE)
    

    Or with ggplot

    library(dplyr)
    library(ggplot2)
    dat %>% 
       filter(time %in% c("Breakfast", "Dinner")) %>%
       droplevels %>%
       count(time, class) %>% 
       group_by(time) %>% 
       mutate(prop = n/sum(n)) %>%
       ggplot(aes(x = class, y = prop, fill = time, label = scales::percent(prop))) +
       geom_col(position = 'dodge') +
       geom_text(position = position_dodge(width = 0.9), vjust = 0.5, size = 3) + 
       scale_y_continuous(labels = scales::percent)