haleyjeppson / ggmosaic

Mosaicplots in the ggplot2 framework
https://haleyjeppson.github.io/ggmosaic/
167 stars 21 forks source link

FILL with other variable than x and y variables #15

Open antuki opened 6 years ago

antuki commented 6 years ago

Hello,

Thank you for this ggplot2 extension !

Mosaic plots are very useful in statistics to illustrate results of khi2 tests (show intensity of correlation between two modalities thanks to the value of residuals).

Here is for example with base R mosaicplot function :

library(tidyverse)
library(ggmosaic)
mosaicplot(happy$marital~ happy$sex, shade = TRUE, main = "Title")

rplot3

I tried to do the same with ggmosaic but I cannot find a way to do it :(

The main issue is that I cannot find a way to use the field "fill" with other variables than the x or y value without obtaining strange outputs... Below is an example with the "var_fill" categorical variable.

library(tidyverse)
library(ggmosaic)

happy2 <- happy %>%
  group_by(sex,marital) %>%
  summarise(count = n()) %>%
  ungroup() %>%
  mutate(var_fill= LETTERS[sample.int(3, 12, replace = TRUE)])

> head(happy2)
# A tibble: 6 x 4
     sex       marital count var_fill
  <fctr>        <fctr> <int>    <chr>
1   male       married 13343        B
2   male never married  5204        A
3   male      divorced  2354        C
4   male       widowed   904        A
5   male     separated   627        B
6   male          <NA>     7        C

ggplot(data=happy2) +
  geom_mosaic(aes(weight=count, x=product(sex, marital), fill=sex)) +
  scale_y_productlist()

rplot1

Strange output when using another "fill" variable (many colors in each box instead of lines with small lines)

ggplot(data=happy2) +
  geom_mosaic(aes(weight=count, x=product(sex, marital), fill=var_fill)) +
  scale_y_productlist()

rplot2

It would be great if someone could help me to find out how I could modify the "fill" legend so that I can use another variable related to the importance of the residuals. Or even better so that I can plot for each box (var_x * var_y) the values of the residuals or the khi2 test (numeric variable which could be modify with scale_fill_continuous).

Thank you for your help !

yiluheihei commented 5 years ago

@antuki Have you solved this problem

antuki commented 5 years ago

I'm sorry, it was 2 years ago and I can't remember. but probably not!

yiluheihei commented 4 years ago

Thanks.

avephill commented 1 year ago

Am also frustrated by this in ggmosaic 0.3.3