0

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

user1420372
  • 2,077
  • 3
  • 25
  • 42
  • As I do not undestand fully what you wanna do so we can start here `ggplot(df,aes(weight=cut.count, x = cut, y = prop, fill = clarity)) + geom_bar(stat = "identity")` what do you want to change here – Mateusz1981 Jan 16 '18 at 07:18
  • The widths of the bar are the same and not in proportion to cut.count. – user1420372 Jan 16 '18 at 11:14

0 Answers0