I am trying to understand how to use geom_mosaic with fill based on a proportion and the width of the bar based on the number of observations. Basically I would like to recreate the answer given by @Z.Lin on the post How to create a Marimekko/Mosaic plot in ggplot2
The data is as follows:
df <- diamonds %>%
group_by(cut, clarity) %>%
summarise(count = n()) %>%
mutate(cut.count = sum(count),
prop = count/sum(count)) %>%
ungroup()
So I would like:
- 1 bar per cut
- fill colour according to clarity (but fill height according to prop)
- width of bar defined by cut.count
My attempt, below, does not scale the fill height by prop (so essentially the y argument is ignored), I have tried supplying stat="identity" to geom_mosaic but receive an error message regarding ymin, ymax, xmin, xmax.
ggplot(df,aes(weight=cut.count, x = product(cut), y = product(prop), fill = clarity)) +
geom_mosaic()
My real data is also pre-summarised