-
Notifications
You must be signed in to change notification settings - Fork 153
/
mosaic.Rmd
189 lines (124 loc) · 5.9 KB
/
mosaic.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# (PART) Multivariate Categorical {-}
# Chart: Mosaic {#mosaic}
<!-- Chapter Banner -->
![](images/banners/banner_mosaic.png)
*This chapter originated as a community contribution created by [harin](https://github.com/harin){target="_blank"}*
*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}
df = read_csv("data/MusicIcecream.csv")
```
## Overview
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.
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()`.
The data:
``` {r}
df
```
Split on `Age` only:
``` {r}
vcd::mosaic(~Age, df)
```
Split on `Age`, then `Music`:
```{r}
vcd::mosaic(Music ~ Age, df)
```
Note that the *first* split is between "young" and "old", while the second set of splits divides each age group into "classical" and "rock".
Split on `Age`, then `Music`, then `Favorite`:
```{r}
vcd::mosaic(Favorite ~ Age + Music, df)
```
## Direction of splits
Note that in the previous example, the direction of the splits is as follows:
1. `Age` -- horizontal split
2. `Music` -- vertical split
3. `Favorite` -- horizontal split
This is the default direction pattern: alternating directions beginning with horizontal. Therefore we get the same plot with the following:
```{r}
vcd::mosaic(Favorite ~ Age + Music,
direction = c("h", "v", "h"), df)
```
The directions can be altered as desired. For example, to create a doubledecker plot, make all splits vertical except the last one:
```{r}
vcd::mosaic(Favorite ~ Age + Music,
direction = c("v", "v", "h"), df)
```
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 "~".
## 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("grey90", "cornflowerblue"),
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"}
### Rotate labels
The `rot_labels =` vector sets the rotation in degrees on the four sides of the plot--not on variable split order--in this order: *top, right, bottom, left*. (Different from the typical base graphics order!) The default is `rot_labels = c(0, 90, 0, 90)`.
```{r}
vcd::mosaic(Favorite ~ Age + Music,
labeling = vcd::labeling_border(rot_labels = c(45, -45, 0, 0)),
df)
```
### Abbreviate labels
Labels are abbreviated in the order of the splits (as for `direction =`). The abbreviation algorithm appears to return the specified number of characters after vowels are eliminated (if necessary).
For more formatting options, see `>?vcd::labeling_border`.
```{r}
vcd::mosaic(Favorite ~ Age + Music,
labeling = vcd::labeling_border(abbreviate_labs = c(3, 1, 6)),
df)
```
## Cell spacing
```{r}
vcd::mosaic(Favorite ~ Age + Music,
spacing = vcd::spacing_equal(sp = unit(0, "lines")),
df)
```
For more details, see `>?vcd::spacings`
### Mosaic using vcd::doubledecker
```{r}
data(Arthritis, package = "vcd")
vcd::doubledecker(Improved ~ Treatment + Sex, data=Arthritis)
```
```{r}
vcd::doubledecker(Music ~ Favorite + Age,
xtabs(Freq ~ Age + Music + Favorite, df))
```
## Mosaic using ggplot
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, eval=FALSE, echo=FALSE}
library(ggmosaic)
# equivalent to doing Favorite ~ Age + Music in vcd::mosaic with doubledecker style cut
ggplot(df) +
geom_mosaic(
aes(x=product(Favorite, Age, Music), # cut from right to left
weight=Freq,
fill=Favorite
),
divider=c("vspine" , "hspine", "hspine") # equivalent to divider=ddecker()
)
```
## Theory
## When to use
When you want to see the relationships in Multivariate Categorical Data
## Considerations
### Labels
Legibility of the labels is problematic in mosaic plot especially when there are a lot of dimensions. This can be alleviated by
- Abbreviate names
- Rotating the labels
### Aspect Ratio
- lengths are easier to judge than area, so try to use rectangles with same width or height
- Taller thinner rectangles are better (we are better at distinguishing length than area)
### Gaps between rectangles
- No gap = most efficient
However, a gap can help improve legibility, so try out different combinations
- Can have a gap at splits
- Can Vary gap size down the hierarchy
### Color
- good for rates in the subgroup
- displaying residual
- emphasizing particular subgroup
## External resources
- Chapter 7 of [Graphical data analysis with R by Anthony Unwin](https://www.crcpress.com/Graphical-Data-Analysis-with-R/Unwin/p/book/9781498715232){target="_blank"}
- [Link](https://cran.r-project.org/web/packages/ggmosaic/vignettes/ggmosaic.html){target="_blank"}: A comprehensive overview of mosaic plot in ggplot check out the link below.