Skip to content

Commit

Permalink
docs: Update README
Browse files Browse the repository at this point in the history
* Change {ggpattern} hex example to use multiple polygon tilings.
* Fix (third) {ggpattern} developing patterns link.
  • Loading branch information
trevorld committed Jan 12, 2022
1 parent 275e9ef commit a175222
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
24 changes: 11 additions & 13 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ grid.pattern_weave(x_hex, y_hex, type = "satin",

## <a name="ggpattern">Using these patterns with the "ggpattern" package</a>

The patterns provided by ``gridpattern`` can be used to create [custom ggpattern geometry-based patterns](https://coolbutuseless.github.io/package/ggpattern/articles/developing-patterns-2.html).
The patterns provided by ``gridpattern`` can be used to create [custom ggpattern geometry-based patterns](https://coolbutuseless.github.io/package/ggpattern/articles/developing-patterns.html).

Here is an example of creating a new "multicolor_stripe" pattern that supports comma-separated ``pattern_fill`` and/or ``pattern_colour`` aesthetics:

Expand Down Expand Up @@ -171,26 +171,21 @@ ggplot(df, aes(trt, outcome)) +
pattern_fill = "grey30,grey70,white,grey70")
```

And here is an example of creating a "hex_tile" pattern that creates
a three-color hexagonal tiling using the ``fill`` colour, the ``pattern_fill`` colour,
And here is an example of creating a "tiling3" pattern that creates
three-color polygon tilings using the ``fill`` colour, the ``pattern_fill`` colour,
and their "average" color.

```{r hex_ggpattern}
hex_pattern <- function(params, boundary_df, aspect_ratio, legend = FALSE) {
tiling3_pattern <- function(params, boundary_df, aspect_ratio, legend = FALSE) {
args <- as.list(params)
args <- args[grep("^pattern_", names(args))]
# hexagonal tiling using "regular_polygon" pattern
args$pattern <- "regular_polygon"
args$pattern_density <- 1
args$pattern_shape <- "convex6"
args$pattern_grid <- "hex"
if (legend) args$pattern_spacing <- 0.5 * args$pattern_spacing
args$pattern <- "polygon_tiling"
# three-color tiling using `fill`, `pattern_fill` and their "average"
avg_col <- gridpattern::mean_col(params$fill, params$pattern_fill)
args$pattern_fill <- c(args$pattern_fill, avg_col, params$fill)
args$pattern_fill <- c(params$fill, avg_col, args$pattern_fill)
args$x <- boundary_df$x
args$y <- boundary_df$y
Expand All @@ -200,10 +195,13 @@ hex_pattern <- function(params, boundary_df, aspect_ratio, legend = FALSE) {
do.call(gridpattern::patternGrob, args)
}
options(ggpattern_geometry_funcs = list(hex_tile = hex_pattern))
options(ggpattern_geometry_funcs = list(tiling3 = tiling3_pattern))
df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))
ggplot(df, aes(trt, outcome)) +
geom_col_pattern(aes(fill = trt), pattern = 'hex_tile', pattern_angle = 45)
geom_col_pattern(aes(fill = trt, pattern_type = trt),
pattern = 'tiling3', pattern_angle = 45) +
scale_pattern_type_manual(values = c("hexagonal", "tetrakis_square", "rhombille")) +
theme(legend.key.size = unit(1.5, 'cm'))
```

## <a name="piecepackr">Using these patterns with the "piecepackr" package</a>
Expand Down
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ grid.pattern_weave(x_hex, y_hex, type = "satin",

## <a name="ggpattern">Using these patterns with the "ggpattern" package</a>

The patterns provided by ``gridpattern`` can be used to create [custom ggpattern geometry-based patterns](https://coolbutuseless.github.io/package/ggpattern/articles/developing-patterns-2.html).
The patterns provided by ``gridpattern`` can be used to create [custom ggpattern geometry-based patterns](https://coolbutuseless.github.io/package/ggpattern/articles/developing-patterns.html).

Here is an example of creating a new "multicolor_stripe" pattern that supports comma-separated ``pattern_fill`` and/or ``pattern_colour`` aesthetics:

Expand Down Expand Up @@ -201,27 +201,22 @@ ggplot(df, aes(trt, outcome)) +

![](man/figures/README-ggpattern-1.png)

And here is an example of creating a "hex_tile" pattern that creates
a three-color hexagonal tiling using the ``fill`` colour, the ``pattern_fill`` colour,
And here is an example of creating a "tiling3" pattern that creates
three-color polygon tilings using the ``fill`` colour, the ``pattern_fill`` colour,
and their "average" color.


```r
hex_pattern <- function(params, boundary_df, aspect_ratio, legend = FALSE) {
tiling3_pattern <- function(params, boundary_df, aspect_ratio, legend = FALSE) {
args <- as.list(params)
args <- args[grep("^pattern_", names(args))]

# hexagonal tiling using "regular_polygon" pattern
args$pattern <- "regular_polygon"
args$pattern_density <- 1
args$pattern_shape <- "convex6"
args$pattern_grid <- "hex"

if (legend) args$pattern_spacing <- 0.5 * args$pattern_spacing
args$pattern <- "polygon_tiling"

# three-color tiling using `fill`, `pattern_fill` and their "average"
avg_col <- gridpattern::mean_col(params$fill, params$pattern_fill)
args$pattern_fill <- c(args$pattern_fill, avg_col, params$fill)
args$pattern_fill <- c(params$fill, avg_col, args$pattern_fill)

args$x <- boundary_df$x
args$y <- boundary_df$y
Expand All @@ -231,10 +226,13 @@ hex_pattern <- function(params, boundary_df, aspect_ratio, legend = FALSE) {
do.call(gridpattern::patternGrob, args)
}

options(ggpattern_geometry_funcs = list(hex_tile = hex_pattern))
options(ggpattern_geometry_funcs = list(tiling3 = tiling3_pattern))
df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))
ggplot(df, aes(trt, outcome)) +
geom_col_pattern(aes(fill = trt), pattern = 'hex_tile', pattern_angle = 45)
geom_col_pattern(aes(fill = trt, pattern_type = trt),
pattern = 'tiling3', pattern_angle = 45) +
scale_pattern_type_manual(values = c("hexagonal", "tetrakis_square", "rhombille")) +
theme(legend.key.size = unit(1.5, 'cm'))
```

![](man/figures/README-hex_ggpattern-1.png)
Expand Down

0 comments on commit a175222

Please sign in to comment.