-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.Rmd
170 lines (128 loc) · 4.61 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
---
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-",
fig.width = 5, fig.height = 1
)
```
# tinter <img src="man/figures/logo.png" align="right" width='20%'/>
[![lifecycle](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://www.tidyverse.org/lifecycle/#maturing)
[![R build status](https://github.com/poissonconsulting/tinter/workflows/R-CMD-check/badge.svg)](https://github.com/poissonconsulting/tinter/actions)
[![Coverage status](https://codecov.io/gh/poissonconsulting/tinter/branch/master/graph/badge.svg)](https://codecov.io/github/poissonconsulting/tinter?branch=master)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
[![CRAN status](https://www.r-pkg.org/badges/version/tinter)](https://cran.r-project.org/package=tinter)
![CRAN Downloads](http://cranlogs.r-pkg.org/badges/grand-total/tinter)
### Introduction
`tinter` provides a simple way to generate monochromatic palettes. Easily define:
+ palette direction ("shades", "tints", or "both").
+ number of colours to generate on either side (`steps`).
+ number of colours to remove from extreme end(s) of palette (e.g. default `crop = 1` eliminates black and white).
+ darker or lighter output (`adjust`).
```{r global}
library(tinter)
hex <- "#335CAC"
```
```{r colour, echo = FALSE, fig.width = 2, fig.height = 0.5}
tinter_plot <- function(x) {
grid <- c(length(x), 1)
width <- 0.9 / (max(grid) + 1)
gap <- 1 / (max(grid) + 1)
centres <- lapply(grid, function(i) {
gap * ((max(grid) -
i) / 2 + seq_len(i))
})
centres <- as.matrix(expand.grid(centres))
oldPars <- graphics::par(mai = c(0, 0, 0, 0), bg = "white")
on.exit(graphics::par(oldPars))
devSize <- grDevices::dev.size()
devRatio <- devSize[2] / devSize[1]
graphics::plot(NA, NA,
xlim = c(-0.1, 1.1), ylim = 0.5 + c(-1, 1) *
devRatio * 0.6, xlab = "", ylab = "", xaxt = "n", yaxt = "n",
bty = "n", asp = 1
)
graphics::rect(centres[, 1] - width / 2, rev(centres[, 2]) - width / 2,
centres[, 1] + width / 2, rev(centres[, 2]) + width / 2,
col = x, border = "white", lwd = 0.2
)
}
tinter_plot(hex)
```
```{r}
tinter(hex)
```
```{r tinter, echo=FALSE}
tinter_plot(tinter(hex))
```
```{r, results="hide"}
tinter(hex, direction = "tints")
```
```{r tints, echo=FALSE}
tinter_plot(tinter(hex, direction = "tints"))
```
```{r, results="hide"}
tinter(hex, steps = 10)
```
```{r steps, echo=FALSE}
tinter_plot(tinter(hex, steps = 10))
```
```{r, results="hide"}
tinter(hex, steps = 10, crop = 7)
```
```{r crop, echo=FALSE}
tinter_plot(tinter(hex, steps = 10, crop = 7))
```
```{r, results="hide"}
tinter(hex, steps = 10, crop = 7, adjust = 0.4)
```
```{r darken, echo=FALSE}
tinter_plot(tinter(hex, steps = 10, crop = 7, adjust = 0.4))
```
### Create a choropleth map
```{r plot, fig.height = 4, fig.width = 8, message=FALSE, warning=FALSE, results="hide"}
library(ggplot2)
library(sf)
nc <- st_read(system.file(package = "sf", "shape/nc.shp"))
ggplot(data = nc) +
geom_sf(aes(fill = AREA), colour = "white", lwd = 0.04) +
# colours from tinter
scale_fill_gradientn(colours = tinter(hex)) +
theme_void() +
coord_sf(datum = NA)
```
### Doesn't this already exist?
`tinter` just simplifies a task usually done with `grDevices`. It's default is to remove black and white from the palette.
```{r}
tinter("blue")
### ------ is identical to
grDevices::colorRampPalette(colors = c("white", "blue", "black"))(11)[-(c(1, 11))]
```
```{r}
tinter("blue", direction = "shades")
### --- is identical to
grDevices::colorRampPalette(colors = c("blue", "black"))(6)[-6]
```
```{r}
tinter("blue", crop = 2)
### --- is identical to
grDevices::colorRampPalette(colors = c("white", "blue", "black"))(11)[-(c(1:2, 10:11))]
```
## Installation
To install the latest release from [CRAN](https://cran.r-project.org)
```{r, eval=FALSE}
install.packages("tinter")
```
To install the developmental version from [GitHub](https://github.com/poissonconsulting/tinter)
```{r, eval=FALSE}
# install.packages("remotes")
remotes::install_github("poissonconsulting/tinter")
```
## Contribution
Please report any [issues](https://github.com/poissonconsulting/tinter/issues).
[Pull requests](https://github.com/poissonconsulting/tinter/pulls) are always welcome.
Please note that the tinter project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.