-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathREADME.Rmd
104 lines (83 loc) · 3.13 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
# relayer
Rethinking layers in ggplot2, written by Claus O. Wilke
## Installation
```
devtools::install_github("clauswilke/relayer")
```
This is an experimental package. Use at your own risk. API is not stable. No user support provided.
## Examples
Load required packages:
```{r message = FALSE}
library(ggplot2) # version 2.3.0 required
library(dplyr)
library(relayer)
```
Make a layer with its own fill scale:
```{r}
df1 <- data.frame(x = c(1, 2, 3))
ggplot(df1, aes(x = x)) +
geom_tile(aes(fill = x, y = 2)) +
geom_tile(aes(fill2 = x, y = 1)) %>% rename_geom_aes(new_aes = c("fill" = "fill2")) +
scale_colour_viridis_c(aesthetics = "fill", guide = "legend", name = "viridis A", option = "A") +
scale_colour_viridis_c(aesthetics = "fill2", guide = "legend", name = "viridis D")
```
Make several layers with their own color scales:
```{r fig.width = 10}
# make color scale shortcut
scale_distiller <- function(aesthetics, palette, name, ...) {
scale_colour_distiller(
aesthetics = aesthetics,
palette = palette,
name = name, ...,
guide = guide_legend(order = palette))
}
# subset data
iris_setosa <- filter(iris, Species == "setosa")
iris_versicolor <- filter(iris, Species == "versicolor")
iris_virginica <- filter(iris, Species == "virginica")
# plot
ggplot(mapping = aes(Sepal.Length, Sepal.Width)) +
(geom_point(data = iris_setosa, aes(colour1 = Sepal.Width)) %>%
rename_geom_aes(new_aes = c("colour" = "colour1"))) +
(geom_point(data = iris_versicolor, aes(colour2 = Sepal.Width)) %>%
rename_geom_aes(new_aes = c("colour" = "colour2"))) +
(geom_point(data = iris_virginica, aes(colour3 = Sepal.Width)) %>%
rename_geom_aes(new_aes = c("colour" = "colour3"))) +
facet_wrap(~Species) +
scale_distiller("colour1", 5, "setosa") +
scale_distiller("colour2", 6, "versicolor") +
scale_distiller("colour3", 7, "virginica") +
theme_bw() +
theme(legend.position = "bottom")
```
Use multiple color scales within a single layer. Note the difference to the previous example: Now the three color scales are jointly trained to the union of the data values.
```{r fig.width = 10}
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(aes(col1 = Sepal.Width, col2 = Sepal.Width, col3 = Sepal.Width, group = Species)) %>%
rename_geom_aes(
new_aes = c("colour" = "col1", "colour" = "col2", "colour" = "col3"),
aes(colour = case_when(group == 1 ~ col1, group == 2 ~ col2, TRUE ~ col3))
) +
facet_wrap(~Species) +
scale_distiller("col1", 5, "setosa") +
scale_distiller("col2", 6, "versicolor") +
scale_distiller("col3", 7, "virginica") +
theme_bw() + theme(legend.position = "bottom", legend.key.width = grid::unit(6, "pt"))
```
Swap `colour` and `fill` aesthetics in a geom:
```{r}
ggplot(iris, aes(Sepal.Length, fill = Species)) +
geom_density(colour = "gray70", size = 2, alpha = 0.7) %>%
rename_geom_aes(new_aes = c("fill" = "colour", "colour" = "fill"))
```