-
Notifications
You must be signed in to change notification settings - Fork 18
/
README.Rmd
113 lines (82 loc) · 3.56 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
---
output: github_document
knit: (function(input, ...) devtools::build_readme())
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
# ggnewscale <img src='man/figures/logo.png' align="right" height="138.5" />
<!-- badges: start -->
[![R build
status](https://github.com/eliocamp/ggnewscale/workflows/R-CMD-check/badge.svg)](https://github.com/eliocamp/ggnewscale/actions)
[![Codecov test
coverage](https://codecov.io/gh/eliocamp/ggnewscale/branch/master/graph/badge.svg)](https://app.codecov.io/gh/eliocamp/ggnewscale?branch=master)
[![DOI](https://zenodo.org/badge/161934647.svg)](https://zenodo.org/badge/latestdoi/161934647)
[![CRAN
status](http://www.r-pkg.org/badges/version/ggnewscale)](https://cran.r-project.org/package=ggnewscale)
<!-- badges: end -->
`ggnewscale` tries to make it painless to use multiple scales in `ggplot2`. Although originally intended to use with colour and fill, it should work with any `aes`, such as `shape`, `linetype` and the rest.
[ggnewscale: spend 400% more time tweaking your ggplot!](https://web.archive.org/web/20220511154749/https://twitter.com/mattansb/status/1524415881920528385)
For another way of defining multiple scales, you can also try [relayer](https://github.com/clauswilke/relayer).
## How to install
You can install ggnewscale from CRAN with:
```{r CRAN-installation, eval = FALSE}
install.packages("ggnewscale")
```
Or the development version with:
```r
# install.packages("devtools")
devtools::install_github("eliocamp/ggnewscale")
```
## How to cite
If you use ggnewscale in a publication, I'll be grateful if you cited it. To get the suggested citation for this (and any other R package) you can use:
```{r}
citation("ggnewscale")
```
If you use knitr, [you can automate this](https://bookdown.org/yihui/rmarkdown-cookbook/write-bib.html) with
```r
knitr::write_bib(c("ggnewscale"), "packages.bib")
```
And then add citations with `@R-ggnewscale`.
<details>
<summary>Click to see a list of some publications that have cited ggnewscale. Thanks!</summary>
```{r echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
bib <- "ggnewscale-cited.bib"
RefManageR::ReadBib(bib)
```
</details>
## Usage
The main function is `new_scale()` and its aliases `new_scale_color()` and `new_scale_fill()`. When added to a plot, every geom added after them will use a different scale.
As an example, let's overlay some measurements over a contour map of topography using the beloved `volcano`.
```{r}
library(ggplot2)
library(ggnewscale)
# Equivalent to melt(volcano)
topography <- expand.grid(x = 1:nrow(volcano),
y = 1:ncol(volcano))
topography$z <- c(volcano)
# point measurements of something at a few locations
set.seed(42)
measurements <- data.frame(x = runif(30, 1, 80),
y = runif(30, 1, 60),
thing = rnorm(30))
ggplot(mapping = aes(x, y)) +
geom_contour(data = topography, aes(z = z, color = stat(level))) +
# Color scale for topography
scale_color_viridis_c(option = "D") +
# geoms below will use another color scale
new_scale_color() +
geom_point(data = measurements, size = 3, aes(color = thing)) +
# Color scale applied to geoms added after new_scale_color()
scale_color_viridis_c(option = "A")
```
If you want to create new scales for other `aes`, you can call `new_scale` with the name of the `aes`. For example, use
```{r, eval = FALSE}
new_scale("linetype")
```
to add multiple linetype scales.