Mosaic Plot similar to R Cookbook

63 Views Asked by At

I want to create a mosaic plot like the one below from the R cookbook, but I cannot get it to plot the third dimension or format properly.

Mosaic Plot from R Cookbook - what I am aiming for

I originally had my data in wide format and I could get a mosaic plot using (vcd) for a two by two table mosaic plot for two columns but what I really wanted is all my columns plotted side by side with the same column to split the data in the y-direction, like the cookbook picture.

A similar issue to this question but not exactly the same I tried this answer got error:

Error in observed - expected : non-numeric argument to binary operator In addition: Warning message: In loglin(x, expected, fit = TRUE, print = FALSE) : NAs introduced by coercion

I transformed my data into long format but it is still not working. I have approximately 17,000 rows by 3 columns all factored long data factors each category within variable has the same number of case and control days but a different count of 1's and 0's which is what I am interested in comparing.

Currently my code is

mosaicplot(table(long$value, long$variable, long$nAP_CAT), col=c("darkblue","red"))

producing a hideous plot

And my data frame (df=long) sample looked like this in wide format

A tibble: 6 x 10

first few rows and columns of df in wide format each column has two factor layers I used melt(long, id.var="nAP_CAT") to make it a long df.

Ideally I want control Day v Case Day in the Y direction and variable categories in the x direction, two per variable category 1 and 0. I would appreciate any help - not an experienced R programmer and stuck, thanks.

0

There are 0 best solutions below