-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
232 lines (188 loc) · 11.4 KB
/
README.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include=FALSE, message=FALSE, warning=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE,
collapse = TRUE,
comment = "#>")
library(dplyr)
library(magrittr)
library(tibble)
library(ggasym)
library(ggplot2)
set.seed(0)
```
# ggasym <a href="https://jhrcook.github.io/ggasym/index.html"> <img src="man/figures/logo.png" align="right" alt="" width="120" /> </a>
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
[![CRAN status](https://www.r-pkg.org/badges/version/ggasym)](https://cran.r-project.org/package=ggasym)
[![CRAN downloads](http://cranlogs.r-pkg.org/badges/grand-total/ggasym)](https://cran.r-project.org/package=ggasym)
[![Travis build status](https://travis-ci.org/jhrcook/ggasym.svg?branch=master)](https://travis-ci.org/jhrcook/ggasym)
[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/jhrcook/ggasym?branch=master&svg=true)](https://ci.appveyor.com/project/jhrcook/ggasym)
[![Coverage status](https://codecov.io/gh/jhrcook/ggasym/branch/master/graph/badge.svg)](https://codecov.io/github/jhrcook/ggasym?branch=master)
'ggasym' (pronounced "gg-awesome") plots a symmetric matrix with three different fill aesthetics for the top-left and bottom-right triangles and along the diagonal. It operates within the Grammar of Graphics paradigm implemented in ['ggplot2'](https://ggplot2.tidyverse.org).
**author: Joshua H. Cook**
**date: 2019-02-22**
**Asymmetric Matrix Plotting in ggplot**
Checkout the documentation and vignettes at the pkgdown website
[https://jhrcook.github.io/ggasym/](https://jhrcook.github.io/ggasym/index.html)
For information on using 'ggplot2', start [here](https://ggplot2.tidyverse.org/index.html).
## Download and Installation
You can download and install from the GitHub repo. Eventually, this will hopefully be available on CRAN, too.
```{r devtools_download, eval = FALSE}
devtools::install_github("jhrcook/ggasym")
```
And load the package with the standard `library` function.
```{r load_lib}
library(ggasym)
```
## Basic Usage
Here is a basic example. `tib` is a "tibble" (ie. fancy "data.frame") of comparisons between groups "A" through "E". There are two values to be plotted, `val_1` and `val_2`, that hold data on the comparison between `g1` and `g2`. `tib` is first passed to `asymmetrise()` to fill in all the missing combinations between `g1` and `g2` such that the symmetric matrix can be built. All added values take the value `NA`. The modified data table is finally passed to `ggplot()` and `geom_asymmat()` is added on. Here, `asymmetrise()` added the rows where `g1` and `g2` are equal, thus will fill the diagonal. I set these values to `val_3`.
```{r example1}
tib <- tibble(g1 = c("A", "A", "A", "A", "B", "B", "B", "C", "C", "D"),
g2 = c("B", "C", "D", "E", "C", "D", "E", "D", "E", "E"),
val_1 = seq(1, 10, 1),
val_2 = rnorm(10, mean = 0, sd = 3))
tib <- asymmetrise(tib, g1, g2)
tib$val_3 <- runif(nrow(tib))
ggplot(tib, aes(x = g1, y = g2)) +
geom_asymmat(aes(fill_tl = val_1, fill_br = val_2, fill_diag = val_3)) +
scale_fill_tl_gradient(low = "lightpink", high = "tomato") +
scale_fill_br_gradient(low = "lightblue1", high = "dodgerblue") +
scale_fill_diag_gradient(low = "yellow", high = "orange3")
```
## New Aesthetics
The new aesthetics `fill_tl`, `fill_br`, and `fill_diag` behave just like the normal `fill`, except that they correspond to the top-left ("tl") and bottom-right ("br") triangles of the matrix, respectively. This package also includes analogous functions for scaling the fill colors such as `scale_fill_tl_gradient2()` and `scale_fill_br_gradientn()` that operate just as expected when using 'ggplot2'.
```{r example2}
ggplot(tib, aes(x = g1, y = g2)) +
geom_asymmat(aes(fill_tl = val_1, fill_br = val_2, fill_diag = val_3)) +
scale_fill_tl_gradient(low = "lightpink", high = "tomato") +
scale_fill_br_gradient2(low = "orange", mid = "white", high = "dodgerblue") +
scale_fill_diag_gradientn(colors = rainbow(25))
```
### Adjusting Colorbars
Of note, with three colorbars, it may be useful to control their position and other properties. This can be done just like normal in 'ggplot2' by passing the correct values to the `guide` parameter in `scale_fill_*_gradient()` ([original documentation](https://ggplot2.tidyverse.org/reference/guide_colourbar.html)). Below are a few of the options where I put the bars horizontal, adjust the ordering, and put the title above each.
```{r adj_colorbars}
ggplot(tib, aes(x = g1, y = g2)) +
geom_asymmat(aes(fill_tl = val_1, fill_br = val_2, fill_diag = val_3)) +
scale_fill_tl_gradient(low = "lightpink", high = "tomato",
guide = guide_colourbar(direction = "horizontal",
order = 1,
title.position = "top")) +
scale_fill_br_gradient2(low = "orange", mid = "white", high = "dodgerblue",
guide = guide_colourbar(direction = "horizontal",
order = 3,
title.position = "top")) +
scale_fill_diag_gradientn(colors = rainbow(25),
guide = guide_colourbar(direction = "horizontal",
order = 2,
title.position = "top"))
```
## Full ggplot2 integration
Since the new geom is a normal 'ggplot2' object, it can be introduced into a standard 'ggplot2' workflow. Note that the labels can be adjusted like normal using the `labs` function and using the `fill_tl`, `fill_br`, and `fill_diag` arguments.
```{r example3}
ggplot(tib, aes(x = g1, y = g2)) +
geom_asymmat(aes(fill_tl = log(val_1),
fill_br = val_2,
fill_diag = val_3)) +
scale_fill_tl_gradient(low = "lightpink", high = "tomato",
guide = guide_colourbar(direction = "horizontal",
order = 1,
title.position = "top")) +
scale_fill_br_gradient(low = "lightblue1", high = "dodgerblue",
guide = guide_colourbar(direction = "horizontal",
order = 3,
title.position = "top")) +
scale_fill_diag_gradient(low = "grey80", high = "grey20",
guide = guide_colourbar(direction = "horizontal",
order = 2,
title.position = "top")) +
labs(fill_tl = "top-left fill",
fill_br = "bottom-right fill",
fill_diag = "diagonal fill",
title = "Example of ggasym") +
theme_bw() +
theme(axis.title = element_blank(),
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "grey70"),
panel.grid = element_blank()) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0))
```
## Facetting
If you have multiple categories, facetting works as expected. The only difference is in the preparation of the data table: you must `group_by()` the value(s) you will facet by before passing to `asymmetrise()`. This is shown below.
```{r facetting_setup}
tib <- tibble(g1 = rep(c("A", "A", "B"), 2),
g2 = rep(c("B", "C", "C"), 2),
val_1 = seq(1, 6),
val_2 = rnorm(6),
grps = c(1, 1, 1, 2, 2, 2))
tib
```
Grouping first by `grps`, the tibble is asymmetrized while retaining the `grps` assignments. I then added values to the diagonal.
```{r grouped_asymm}
tib <- tib %>% group_by(grps) %>% asymmetrise(g1, g2) %>% ungroup()
tib <- tib %>% mutate(val_3 = ifelse(g1 == g2, runif(nrow(tib)), NA))
tib
```
```{r facetting_plot}
ggplot(tib, aes(x = g1, y = g2)) +
geom_asymmat(aes(fill_tl = log(val_1),
fill_br = val_2,
fill_diag = val_3)) +
scale_fill_tl_gradient(low = "lightpink", high = "tomato",
guide = guide_colourbar(direction = "horizontal",
order = 1,
title.position = "top")) +
scale_fill_br_gradient(low = "lightblue1", high = "dodgerblue",
guide = guide_colourbar(direction = "horizontal",
order = 3,
title.position = "top")) +
scale_fill_diag_gradient(low = "grey80", high = "grey20",
guide = guide_colourbar(direction = "horizontal",
order = 2,
title.position = "top")) +
labs(fill_tl = "top-left fill",
fill_br = "bottom-right fill",
fill_diag = "diagonal fill",
title = "Example of facetting with ggasym") +
theme_bw() +
theme(axis.title = element_blank(),
plot.title = element_text(hjust = 0.5),
panel.background = element_rect(fill = "grey70"),
panel.grid = element_blank()) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
facet_grid(. ~ grps)
```
---
## Statistical Test Wrapper
I created a wrapper for handling the results of a statistical test to produce a tibble ready to be plotted with ggasym. Here is a brief example - a more detailed example is shown in the vignette ["Statistical Test Plotting"](https://jhrcook.github.io/ggasym/articles/ggasym-stats.html). Here I test if the median sale price of houses in Austin, Texas is different between any of the years (for more information on the data source: `?ggplot2::txhousing`).
```{r stats_example_prep}
tib <- ggplot2::txhousing %>%
filter(city == "Austin") %>%
mutate(year = as.character(year))
aov_res <- aov(median ~ year, data = tib)
broom::tidy(aov_res)
```
Before plotting, the results of the Tukey post-hoc test are passed to `asymmetrise_stats()` that prepares the data for `geom_asymmat()`. The resulting tibble is then plotted and styled in 'ggplot2'.
```{r stats_example_plot}
asymmat_tib <- asymmetrise_stats(TukeyHSD(aov_res))
ggplot(asymmat_tib, aes(x = x, y = y)) +
geom_asymmat(aes(fill_tl = estimate,
fill_br = -log10(adj.p.value + 0.0000001))) +
scale_fill_tl_gradient2(low = "dodgerblue", high = "tomato") +
scale_fill_br_distiller(type = "seq", palette = "Greens", direction = 1) +
labs(title = "Median House Prices in Austin",
fill_tl = "diff. in\nmean values",
fill_br = "-log10( adj. p-value )") +
theme(panel.background = element_rect(fill = "grey75"),
panel.grid = element_blank()) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0))
```
---
### Thank yous
I would like to thank the team behind ['ggplot2'](https://ggplot2.tidyverse.org) for creating a flexible and powerful package for the R community.
If you see any mistakes (including small typos) *please* open an [issue](https://github.com/jhrcook/ggasym/issues) and leave a quick statement. Do not worry about appearing annoying.