Benjamin Telkamp
Benjamin Telkamp

Reputation: 1561

Adjusting (Stacked) Bar Width according to Proportions of Categories in ggplot2

I'm trying to change my (stacked) bar width according to the counts (or proportion) of the categories, As an example i used the diamonds dataset. I want to see a varying width according to the frequency of each category (of the variable cut). I first created a variable cut_prop and then plotted with the following code

library(tidyverse)

cut_prop = diamonds %>% 
  group_by(cut) %>% 
  summarise(cut_prop = n()/nrow(diamonds))

diamonds = left_join(diamonds, cut_prop)

ggplot(data = diamonds, 
       aes(x = cut, fill = color)) + 
  geom_bar(aes(width=cut_prop), position = "fill") + 
  theme_minimal() +
  coord_flip()

Which gave me the following barplot:

enter image description here

R gives a warning which tells: Ignoring unknown aesthetics: width and obviously doesn't take the proportion of categories for the width of the bars into account, anyone who can help me out here? Thanks!

Upvotes: 4

Views: 2286

Answers (1)

Dan
Dan

Reputation: 12074

I think this works. Starting where you left off...

df <- diamonds %>% 
  count(cut, color, cut_prop) %>% 
  group_by(cut) %>% 
  mutate(freq = n / sum(n)) %>% 
  ungroup

ggplot(data = df,
       aes(x = cut, fill = color, y = freq, width = cut_prop)) +
  geom_bar(stat = "identity") +
  theme_minimal() +
  coord_flip()

enter image description here

Essentially, I calculate the proportions myself instead of using position = "fill", then use stat = identity rather than stat = count.

Upvotes: 5

Related Questions