-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
131 lines (88 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
---
output: github_document
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
dev = 'ragg_png',
fig.width = 8,
fig.height = 4,
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
# fillpattern
<!-- badges: start -->
[![dev](https://github.com/cmmr/fillpattern/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/cmmr/fillpattern/actions/workflows/R-CMD-check.yaml)
[![cran](https://www.r-pkg.org/badges/version/fillpattern)](https://CRAN.R-project.org/package=fillpattern)
[![conda](https://anaconda.org/conda-forge/r-fillpattern/badges/version.svg)](https://anaconda.org/conda-forge/r-fillpattern)
<!-- badges: end -->
## Overview
`fillpattern` streamlines the process of adding distinctive yet unobtrusive geometric patterns in place of solid grob/geom fills. The resultant figures look just as professional when viewed by colorblind readers or when printed in black and white.
Compared to the similar [`ggpattern`](https://coolbutuseless.github.io/package/ggpattern/index.html) package, `fillpattern`:
* Has no dependencies beyond base R and `ggplot2`.
* Works with `ggplot2::geom_*` functions.
* Focuses on simple geometric patterns.
## Installation
```{r, eval = FALSE}
# Install the latest stable version from CRAN:
install.packages("fillpattern")
# Or the development version from GitHub:
install.packages("remotes")
remotes::install_github("cmmr/fillpattern")
```
## Usage
Simply add `scale_fill_pattern()` to your ggplot.
```{r}
library(ggplot2)
library(fillpattern)
ggplot(mpg, aes(x = class, color = drv, fill = drv)) +
geom_bar() +
scale_fill_pattern()
```
Works with `geom_bar()`, `geom_boxplot()`, `geom_violin()`, and other `geom_*` functions that accept a `fill` aesthetic.
### grid grobs
Set `fill = fill_pattern()` in the grob's graphical parameters.
```{r, fig.height=2}
library(grid)
library(fillpattern)
grid.newpage()
grid.circle( gp = gpar(fill = fill_pattern("grid")), x = 1/4, r = 3/8)
grid.rect( gp = gpar(fill = fill_pattern("fish")), width = 1/5, height = 3/4)
grid.polygon(gp = gpar(fill = fill_pattern("brick")), x = c(6,7,5)/8, y = c(7,1,1)/8)
```
## Basic Patterns
Use any of these pattern names in `fill_pattern()` or `scale_fill_pattern()`.
```{r, echo = FALSE}
grid.newpage()
invisible(mapply(
p = c(
"brick", "chevron", "fish", "grid", "herringbone", "hexagon",
"octagon", "saw", "shingle", "rshingle", "stripe", "wave" ),
x = rep(1:4 / 4, times = 3) - 1/8,
y = rep(3:1 / 3, each = 4) - 1/6,
function (p, x, y) {
gp <- gpar(fill = fill_pattern(p))
grid.rect(x = x, y = y - .03, width = .2, height = .2, gp = gp)
grid.text(x = x, y = y + .11, label = p)
}))
```
## Modified Patterns
For each basic pattern, you can specify the foreground color, background color, line width/style, tile size/rotation, and more through arguments to `fill_pattern()` and `scale_fill_pattern()`.
Most modifications can be specified as part of the pattern name (shown below).
```{r, echo = FALSE}
grid.newpage()
invisible(mapply(
p = c(
"brick20", "chevron_2dashed", "fish_dotted", "grid_longdash",
"herringbone45", "hexagon_sm", "octagon_:sm", "saw_16mm:8mm",
"shingle_3lwd", "rshingle_dotted", "stripe35_lg", "wave45_sm" ),
x = rep(1:4 / 4, times = 3) - 1/8,
y = rep(3:1 / 3, each = 4) - 1/6,
function (p, x, y) {
gp <- gpar(fill = fill_pattern(p))
grid.rect(x = x, y = y - .03, width = .2, height = .2, gp = gp)
grid.text(x = x, y = y + .11, label = p)
}))
```
For additional details, see the `fill_pattern()` and `scale_fill_pattern()` reference pages.