Ketty
Ketty

Reputation: 851

How to display summary statistics by group in ggplot2

I have a data fram with 3 variables: manu, brand and vol.

Here is my plot. How can I display the total vol and % vol by manufacturer somewhere in the plot?

My data:

manu <- c('A', 'A', 'A', 'B',"B", "C","C")
brand <- c('A1', 'A2', 'A3', 'B1',"B2", "C1","C2")
vol <- c(10,    25, 30, 45, 15, 25, 20)

My code:

ggplot2::ggplot(environment=environment()) + ggplot2::geom_bar(ggplot2::aes(y = vol, x = manu, fill = brand), stat = "identity", position = ggplot2::position_stack(reverse = TRUE), data = df)

enter image description here

FINAL ANSWER FOR COMPLETION SAKE:

library("magrittr")
library("dplyr")
library(ggplot2)


manu  <- c('A', 'A', 'A', 'B',"B", "C","C")
brand <- c('A1', 'A2', 'A3', 'B1',"B2", "C1","C2")
vol   <- c(10, 25, 30, 45, 15, 25, 20)

df <- data.frame(manu = c('A', 'A', 'A', 'B',"B", "C","C"),
                 brand = c('A1', 'A2', 'A3', 'B1',"B2", "C1","C2"),
                 vol = c(10, 25, 30, 45, 15, 25, 20))

df$grand_total_vol = sum(df$vol)

df2 <- df %>% group_by(manu) %>% mutate(total_vol = sum(vol)) %>% mutate(percent_vol = 100*total_vol/grand_total_vol)  %>% ungroup()

ggplot(data=df2) +
  geom_bar(aes(x=manu, y=vol, fill=brand),stat = "identity",position = position_stack(reverse = TRUE)) +
  geom_text(aes(x=manu, y=vol, label= paste0("Percent Vol: ", round(percent_vol*100,1),"%")),
            position = position_stack(vjust = .5)) +
  geom_text(aes(x = manu,y = 70, label = paste0(df2$total_vol, "; ", round(df2$percent_vol, 1), "%")))

Upvotes: 0

Views: 937

Answers (1)

daszlosek
daszlosek

Reputation: 1505

Something like this should work. You will have to round/ play with the position of the percentages as you see fit.

install.packages("dplyr")
install.packages("magrittr")
library("magrittr")
library("dplyr")
library(ggplot2)


manu <- c('A', 'A', 'A', 'B',"B", "C","C")
brand <- c('A1', 'A2', 'A3', 'B1',"B2", "C1","C2")
vol <- c(10,    25, 30, 45, 15, 25, 20)

df <- data.frame(manu = c('A', 'A', 'A', 'B',"B", "C","C"),
                 brand = c('A1', 'A2', 'A3', 'B1',"B2", "C1","C2"),
                 vol = c(10, 25, 30, 45, 15, 25, 20))


df2 <- df %>% group_by(manu) %>% mutate(total_vol = sum(vol),
                                        percent_vol = vol/total_vol) %>%
          ungroup()

ggplot(data=df2) + 
     geom_bar(aes(x=manu, y=vol, fill=brand),stat = "identity",position = position_stack(reverse = TRUE)) +
     geom_text(aes(x=manu, y=vol, label= paste0("Percent Vol: ", round(percent_vol*100,1),"%")),
              position = position_stack(vjust = .5)) +
     geom_text(aes(x = manu,y = 70, label = paste0("Total Vol: ",df2$total_vol)))

EDITED

enter image description here

Upvotes: 3

Related Questions