-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
224 lines (167 loc) · 4.91 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
fig.width = 8,
fig.height = 1.2,
out.width = "100%",
dpi = 600
)
```
# ggspruce
<!-- badges: start -->
[![R-CMD-check](https://github.com/sheridar/ggspruce/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/sheridar/ggspruce/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/sheridar/ggspruce/graph/badge.svg)](https://app.codecov.io/gh/sheridar/ggspruce)
<!-- badges: end -->
ggspruce provides functions that make automatic adjustments to ggplot2 plots
to improve aesthetics.
## Installation
You can install the development version of ggspruce from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("sheridar/ggspruce")
```
<br>
## Optimizing color palettes
This is a color palette from the MetBrewer package.
This palette can be improved by modifying colors 2, 12, and 13 to make
them more distinct.
```{r}
library(ggspruce)
library(ggplot2)
library(MetBrewer)
library(cowplot)
clrs <- as.character(met.brewer("Austria", 15))
plot_colors(clrs)
```
The `spruce_colors()` function calculates the pairwise distance between
colors in the palette, and identifies pairs of colors that are very similar.
The difference threshold used to identify similar colors can be modified with
the `difference` parameter, with higher values resulting in colors that are more
distinct.
How colors are modified can be controlled using the `property` parameter.
By default, new colors are interpolated based on the other colors on the
palette.
```{r}
new_clrs <- clrs |>
spruce_colors(difference = 10)
plot_colors(new_clrs)
```
<br>
One or more color properties can also be specified to limit how the colors are
adjusted.
If the example below lightness is first adjusted, if the resulting colors still
do not meet the difference threshold, the hue is adjusted.
```{r}
new_clrs <- clrs |>
spruce_colors(
difference = 20,
property = c("lightness", "hue")
)
plot_colors(new_clrs)
```
<br>
## Integration with ggplot2
Color palettes can be automatically modified on the fly using the
`scale_color_spruce()` and `scale_fill_spruce()` functions.
These functions are similar to the `ggplot2::scale_*_manual()` functions with
some modifications.
Original colors are shown on the left and adjusted colors on the right.
```{r, fig.width = 8, fig.height = 5}
clrs <- as.character(met.brewer("Nattier", 8))
# Original colors
plt <- diamonds |>
ggplot(aes(carat, price, color = clarity)) +
geom_point(size = 1) +
guides(color = guide_legend(override.aes = list(size = 3))) +
theme_bw() +
theme(
aspect.ratio = 1,
legend.position = "bottom"
)
plt1 <- plt +
scale_color_manual(values = clrs) +
ggtitle("original")
# Automatically adjust colors
plt2 <- plt +
scale_color_spruce(
values = clrs,
difference = 25,
property = "lightness"
) +
ggtitle("adjusted")
plot_grid(plt1, plt2, nrow = 1)
```
<br>
If no colors are provided to the `scale_*_spruce()` functions, they will pull
colors from the existing color scale.
```{r, fig.width = 8, fig.height = 5}
# Adjust colors from existing scale
plt1 <- plt +
scale_color_discrete()
plt2 <- plt1 +
scale_color_spruce(difference = 25)
plot_grid(plt1, plt2, nrow = 1)
```
<br>
## Colorblind filters
Color similarity can be assessed and adjusted using colorblind filters.
The original and adjusted palettes are shown below.
```{r, fig.width = 8, fig.height = 3}
library(colorspace)
library(RColorBrewer)
clrs <- RColorBrewer::brewer.pal(10, "RdYlGn")
new_clrs <- clrs |>
spruce_colors(
difference = 15,
property = c("lightness", "saturation"),
filter = "deutan"
)
# Plot adjusted colors without filter
plt1 <- clrs |>
plot_colors() +
ggtitle("original")
plt2 <- new_clrs |>
plot_colors() +
ggtitle("adjusted")
plot_grid(plt1, plt2, ncol = 1)
```
The palettes are shown below using a filter to simulate deuteranopia.
```{r, fig.width = 8, fig.height = 3}
# Plot adjusted colors with colorblind filter
plt3 <- clrs |>
deutan() |>
plot_colors() +
ggtitle("original + filter")
plt4 <- new_clrs |>
deutan() |>
plot_colors() +
ggtitle("adjusted + filter")
plot_grid(plt3, plt4, ncol = 1)
```
```{r "TEST RESIZE PALETTE", eval = FALSE, include = FALSE, echo = FALSE}
test_clrs <- clrs[1:20]
n <- 10
# NEW GENSA
test_clrs |>
collapse_colors(n = n, exact = FALSE) |>
plot_colors()
test_clrs |>
spruce_colors() |>
plot_colors()
collapse_colors(test_clrs, n = n, exact = TRUE)
# OLD
res <- test_clrs |>
collapse_colors(n = n, exact = TRUE)
res |>
plot_colors()
x <- res |>
farver::decode_colour(res, to = "lab") |>
farver::compare_colour(from_space = "lab", method = "CIE2000")
min(x[upper.tri(x)])
```