Search code examples
rggplot2facetsummarize

Get Proportion Graph From Summarise and Facet Wrap


I have three categorical variables and one numeric variable; I want to show proportions by segmenting the data based on my categorical variables and getting the proportions of the numeric variable.

The data is as follows:

ID Brand   Color    Gear Sales
1  Honda   Blue     M    80
2  Toyota  Blue     A    75
3  Ford    Blue     M    25
4  Honda   Red      M    100
5  Toyota  Red      M    125
6  Ford    Red      M    90
7  Honda   Green    A    15
8  Toyota  Green    M    120
9  Ford    Green    A    65

Essentially, I want a bar graph that shows a facet_wrap of the Brand what proportion of sales were each Color.

The result would be For Honda 80 Blue/195 total, 100 Red/ 185 Total and 15 Green/ 185 Total, etc... This percentage shown in a bar graph:

a <-  df%>% group_by(Brand, Color)

b <-  summarise(a, sales_amt = sum(Sales),
                   brand_sale = sum("Here is where I am having the issue"),
                   sales_percentage = (sales_amt/brand_sale))

c <-  ggplot(b) + 
        geom_bar(aes(Color, sales_percentage) , stat = "identity") + 
        facet_wrap(~ Brand)
c

Solution

  • Consider base R's ave for inline aggregation by group for denominator in sales percentage:

    df <- within(df, {       
       brand_sale <- ave(Sales, Brand, FUN=sum)
       sales_percentage <- Sales / brand_sale       
    })
    
    df    
    #   ID  Brand Color Gear Sales sales_percentage brand_sale
    # 1  1  Honda  Blue    M    80       0.41025641        195
    # 2  2 Toyota  Blue    A    75       0.23437500        320
    # 3  3   Ford  Blue    M    25       0.13888889        180
    # 4  4  Honda   Red    M   100       0.51282051        195
    # 5  5 Toyota   Red    M   125       0.39062500        320
    # 6  6   Ford   Red    M    90       0.50000000        180
    # 7  7  Honda Green    A    15       0.07692308        195
    # 8  8 Toyota Green    M   120       0.37500000        320
    # 9  9   Ford Green    A    65       0.36111111        180
    
    c <-  ggplot(df) + 
      geom_bar(aes(Color, sales_percentage) , stat = "identity") + 
      facet_wrap(~ Brand)
    c
    

    Plot Output