Skip to content

Commit

Permalink
use highlighting_fill instead of grid::gpar for fill
Browse files Browse the repository at this point in the history
  • Loading branch information
jtr13 committed Oct 11, 2020
1 parent 3ff918c commit 04fca09
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 48 deletions.
18 changes: 9 additions & 9 deletions data/MusicIcecream.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"Age","Favorite","Music","Freq"
"old","bubble gum","classical",1
"old","bubble gum","rock",1
"old","coffee","classical",3
"old","coffee","rock",1
"young","bubble gum","classical",2
"young","bubble gum","rock",5
"young","coffee","classical",1
"young","coffee","rock",0
Age,Music,Favorite,Freq
old,classical,bubble gum,1
old,rock,bubble gum,1
old,classical,coffee,3
old,rock,coffee,1
young,classical,bubble gum,2
young,rock,bubble gum,5
young,classical,coffee,1
young,rock,coffee,0
49 changes: 10 additions & 39 deletions mosaic.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,15 @@

*This page is a work in progress. We appreciate any input you may have. If you would like to help improve this page, consider [contributing to our repo](contribute.html).*

```{r include=FALSE}
df = read.csv("data/MusicIcecream.csv")
```{r}
df = read_csv("data/MusicIcecream.csv")
```


## Overview

This section covers how to make Mosaic plots

## tl;dr

```{r message=FALSE}
vcd::mosaic(Favorite ~ Age + Music,
labeling = vcd::labeling_border(
abbreviate_labs = c(3, 10, 6),
rot_labels=c(0,0,-45)
),
direction=c('v','v','h'), # Age = Vertical, Music = Vertical, Favoriate = Horizonal (a.k.a DoubleDecker)
gp = grid::gpar(fill=c('lightblue', 'gray')),
df)
```

## Order of splits
Mosaic plots take some investment to learn to read and draw properly. Particularly when starting out, we recommend drawing them incrementally: start with splitting on one variable and then add additional variables one at a time. The full mosaic plot will have one split per variable.

It is best to draw mosaic plots incrementally: start with splitting on one variable and then add additional variables one at a time. The full mosaic plot will have one split per variable.

Important: if your data is in a data frame, as in the example below, **the count column must be called `Freq`**. (Tables and matrices also work, see `?vcd::structable` for more details.)
Important: if your data has a frequency column, as in the example below, **the count column must be called `Freq`**. (Tables and matrices also work, see `?vcd::structable` for more details.)

Also note that all of these plots are drawn with `vcd::mosaic()` not the base R function, `mosaicplot()`.

Expand Down Expand Up @@ -89,27 +71,16 @@ vcd::mosaic(Favorite ~ Age + Music,

Note that the direction vector is in order of splits (`Age`, `Music`, `Favorite`), not in the order in which the variables appear in the formula, where the last variable to be split is listed first, before the "~".

## Color
## Fill color

### Fill color
Fill colors are applied and recycled according to the *last* cut dimension, i.e. the dependent variable--in this case favorite flavor ice cream. (If this is not working properly, update to the [latest version of **vcd**](https://cran.r-project.org/web/packages/vcd/).

```{r}
vcd::mosaic(Favorite ~ Age + Music,
highlighting_fill = c("lightblue", "blue"),
highlighting_fill = c("grey90", "cornflowerblue"),
df)
```

### Border color (must also set fill(?))

```{r}
vcd::mosaic(Favorite ~ Age + Music,
gp = grid::gpar(fill = c("lightblue", "blue"),
col = "white"),
spacing = vcd::spacing_equal(sp = unit(0, "lines")),
df)
```


## Labels

For official documentation on labeling options, see [Labeling in the Strucplot Framework](http://ftp.auckland.ac.nz/software/CRAN/doc/vignettes/vcd/labeling.pdf){target="_blank"}
Expand Down Expand Up @@ -162,12 +133,12 @@ vcd::doubledecker(Music ~ Favorite + Age,

## Mosaic using ggplot

For a comprehensive overview of mosaic plot in ggplot check out the link below.
To create mosaic plots in the **ggplot2** framework, use `geom_mosaic()` which is available in the **ggmosaic** package:

[https://cran.r-project.org/web/packages/ggmosaic/vignettes/ggmosaic.html](https://cran.r-project.org/web/packages/ggmosaic/vignettes/ggmosaic.html){target="_blank"}

```{r message=FALSE}
```{r, eval=FALSE, echo=FALSE}
library(ggmosaic)

# equivalent to doing Favorite ~ Age + Music in vcd::mosaic with doubledecker style cut
ggplot(df) +
geom_mosaic(
Expand Down

0 comments on commit 04fca09

Please sign in to comment.