This repository was archived by the owner on Sep 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathREADME.Rmd
189 lines (137 loc) · 7.47 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
---
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 = "README-"
)
```
```{r, echo = FALSE}
description <- readLines(system.file("DESCRIPTION", package='colormap'))
rvers <- stringr::str_match(grep("R \\(", description, value = TRUE), "[0-9]{1,4}\\.[0-9]{1,4}\\.[0-9]{1,4}")[1,1]
version <- gsub(" ", "", gsub("Version:", "", grep("Version:", description, value = TRUE)))
```
[](http://www.repostatus.org/#active) [)`-green.svg)](/commits/master) [](https://opensource.org/licenses/MIT) [](https://gist.github.com/bhaskarvk/46fbf2ba7b5713151d7e) [](https://travis-ci.org/bhaskarvk/colormap) [](https://ci.appveyor.com/project/bhaskarvk/colormap) [](https://cran.r-project.org/) [](commits/master) [](https://cran.r-project.org/package=colormap) [](http://cran.rstudio.com/web/packages/colormap/index.html)
## Color Palettes from Node.js Colormap module.
This is an R package that allows you to generate colors from color palettes defined in Node.js's [colormap](https://github.com/bpostlethwaite/colormap) module. In total it provides 44 distinct palettes made from sequential and/or diverging colors. In addition to the pre defined palettes you can also specify your own set of colors.
There are also scale functions that can be used with ggplot2.
#### Changelog
- 2016-11-15 0.1.4 With Viridis as default theme.
- 2016-10-21 0.1.3 Now on CRAN.
- 2016-09-06 Ability to generate a custom palette.
- 2016-08-30 Input Validation and ggplot2 scales.
- 2016-08-29 First Release.
### Credits
- The [colormap](https://github.com/bpostlethwaite/colormap) Node.js module which does all the heavylifting.
- The [V8](https://github.com/jeroenooms/V8) package which allows R code to call Javascript code.
- [Bob Rudis](https://twitter.com/hrbrmstr)'s [zoneparser](https://github.com/hrbrmstr/zoneparser) package which I used as a skeleton for this pacakge.
- [Simon Garnier](https://twitter.com/sjmgarnier)'s [viridis](https://github.com/sjmgarnier/viridis) package for ggplot2 scale functions.
### Installation
Requires [V8](https://cran.r-project.org/web/packages/V8/index.html)
```{r install, eval=FALSE}
if(!require("V8")) install.packages("V8")
if(!require("devtools")) install.packages("devtools")
devtools::install_github("bhaskarvk/colormap")
```
### Usage
The main function is `colormap` which takes 5 optional arguments
- colormap: A string representing one of the 44 built-in colormaps.\n You can use the `colormaps` list to specify a value. e.g. `colormaps$density` **OR** A vector of colors in hex e.g. c('#000000','#777777','#FFFFFF') **OR** A list of list e.g. list(list(index=0,rgb=c(255,255,255)),list(index=1,rgb=c(255,0,0)))
- nshades: Number of colors to generate.
- format: one of 'hex', 'rgb', 'rgbaString'
- alpha: Between 0 & 1 to specify the transparency.
- reverse: Boolean. Whether to reverse the order of the colors returned or not.
### Example
```{r eg1}
library(colormap)
# Defaults to 72 colors from the 'viridis' palette.
scales::show_col(colormap(), labels = F)
# Specify a different palette from a list of pre-defined palette.
scales::show_col(colormap(colormap=colormaps$temperature, nshades=20))
# Specify opacity value.
scales::show_col(colormap(colormap=colormaps$temperature, nshades=20, alpha=0.7))
# Specify colormap as vector of colors.
scales::show_col(colormap(colormap=c('#FFFFFF','#FF0000'),nshades = 12))
# Specify colormap as list of lists.
scales::show_col(colormap(colormap=list(list(index=0,rgb=c(0,255,0)),
list(index=1,rgb=c(255,0,255))),
nshades=12, alpha=0.65))
```
You can also get the colors in a 'rgb' matrix and a rgba string vector format
```{r eg2}
colormap(format='rgb',nshades=5) # As rgb
colormap(format='rgbaString',nshades=10) # As rgba string
```
You also get `scale_fill_colormap` and `scale_color_colormap` functions for using these palettes in ggplot2 plots. Check `?colormap::scale_fill_colormap` for details.
```{r ggplot2, fig.width=6, fig.height=4}
ensureCranPkg <- function(pkg) {
if(!suppressWarnings(requireNamespace(pkg, quietly = TRUE))) {
install.packages(pkg)
}
}
ensureCranPkg('ggplot2')
library(ggplot2)
# Continuous color scale
ggplot(mtcars,aes(x=wt,y=mpg)) + geom_point(aes(color=hp)) +
theme_minimal() +
scale_color_colormap('Horse Power',
discrete = F,colormap = colormaps$viridis, reverse = T)
ggplot(mtcars,aes(x=wt,y=mpg)) + geom_point(aes(color=as.factor(cyl))) +
theme_minimal() +
scale_color_colormap('Cylinder',
discrete = T,colormap = colormaps$warm, reverse = T)
```
Here are two choroplethes using `scale_fill_colormap`.
```{r maps}
ensureCranPkg('maptools')
ensureCranPkg('scales')
ensureCranPkg('ggplot2')
ensureCranPkg('ggthemes')
ensureCranPkg('devtools')
if(!suppressWarnings(requireNamespace('albersusa', quietly = TRUE))) {
devtools::install_github('hrbrmstr/albersusa')
}
library(maptools)
library(scales)
library(ggplot2)
library(albersusa)
library(ggthemes)
library(colormap)
us <- usa_composite()
us_map <- fortify(us, region="fips_state")
gg_usa <- ggplot(us@data, aes(map_id=fips_state,fill=pop_2014)) +
geom_map(map=us_map, color='#ffffff', size=0.1) +
expand_limits(x=us_map$long,y=us_map$lat) +
theme_map() +
theme(legend.position="right")
gg_usa +
coord_map("albers", lat0=30, lat1=40) +
scale_fill_colormap("State Population\n(2014 Estimates)", labels=comma,
colormap = colormaps$copper, reverse = T, discrete = F)
counties <- counties_composite()
counties_map <- fortify(counties, region="fips")
gg_counties <- ggplot(counties@data,
aes(map_id=fips,fill=census_area)) +
geom_map(map=counties_map, color='#ffffff', size=0.1) +
expand_limits(x=counties_map$long,y=counties_map$lat) +
theme_map() +
theme(legend.position="right")
gg_counties +
coord_map("albers", lat0=30, lat1=40) +
scale_fill_colormap("County Area", labels=comma, trans = 'log10',
colormap = colormaps$freesurface_red, reverse = T, discrete = F) +
theme(#panel.border = element_rect(colour = "black", fill=NA, size=1),
legend.position = 'bottom', legend.direction = "horizontal")
```
Here is a plot showing all 44 pre-defined color palettes and the colors they generate.
```{r plot, fig.height=18}
ensureCranPkg('purrr')
par(mfrow=c(44,1))
par(mar=rep(0.25,4))
purrr::walk(colormaps, function(x) {
barplot(rep(1,72), yaxt="n", space=c(0,0),border=NA,
col=colormap(colormap=x), main = sprintf("\n\n%s",x))
})
```