Closed ncrnalab closed 2 months ago
pattern_spacing
aesthetic used by many "geometry" pattern is in {grid}
"snpc" units {grid}
viewport of different width so that these {grid}
"snpc" units are being converted into different physical sizespattern_spacing
values to fix this (or use/write patterns that don't depend on "snpc" units):library (tidyverse)
library (ggpattern)
df <- data.frame (x=as.character (1:15),
y=rep (c(1, 3, -5), 5),
pattern = rep (c("n", "s", "s"), 5),
fill = rep (c("r", "b", "r"), 5),
facet = c(rep("x", 10), rep("y", 2), rep("z", 3)))
ggplot (df, aes (x=x, y=y, fill=fill, pattern = pattern)) +
geom_col_pattern (color = "black",
pattern_fill = "black",
pattern_angle = 45,
pattern_density = 0.1,
pattern_spacing = c(rep(0.08, 10), rep(0.32, 2), rep(0.24, 3)),
pattern_key_scale_factor = 0.6) +
facet_grid (~facet, scales="free_x", space="free") +
scale_fill_manual(values = c("red", "blue")) +
scale_pattern_manual(values = c("none", "stripe"))
Here is an example of "custom" circle/stripe patterns that use "cm" units instead of "snpc" units:
library("grid")
library("ggpattern")
library("ggplot2")
cm2snpc <- function(cm) {
max(convertX(unit(cm, "cm"), "npc", valueOnly = TRUE),
convertY(unit(cm, "cm"), "npc", valueOnly = TRUE))
}
cm_pattern <- function(params, boundary_df, aspect_ratio, legend = FALSE) {
args <- as.list(params)
pattern <- gsub("_cm", "", args$pattern)
args <- args[grep("^pattern_", names(args))]
args$pattern <- pattern
if (legend)
args$pattern_spacing <- cm2snpc(0.5)
else
args$pattern_spacing <- cm2snpc(args$pattern_spacing)
args$pattern_xoffset <- cm2snpc(args$pattern_xoffset)
args$pattern_yoffset <- cm2snpc(args$pattern_yoffset)
args$x <- boundary_df$x
args$y <- boundary_df$y
args$id <- boundary_df$id
args$prefix <- ""
do.call(gridpattern::patternGrob, args)
}
options(ggpattern_geometry_funcs = list(circle_cm = cm_pattern,
stripe_cm = cm_pattern))
df <- data.frame (x=as.character (1:15),
y=rep (c(1, 3, -5), 5),
pattern = rep (c("a", "b", "b"), 5),
fill = rep (c("a", "b", "a"), 5),
facet = c(rep("x", 10), rep("y", 2), rep("z", 3)))
ggplot (df, aes (x=x, y=y, fill=fill, pattern=pattern)) +
geom_col_pattern (color = "black",
pattern_fill = "black",
pattern_angle = 45,
pattern_density = 0.1,
pattern_spacing = 0.5) +
facet_grid (~facet, scales="free_x", space="free") +
scale_pattern_manual(values = c("stripe_cm", "circle_cm"))
Possible "solutions" for when facet_grid(space="free")
:
Use "cm" or "in" instead of "snpc" units as default
Keep "snpc" as "default" but let users use a grid::unit()
to set a different "unit"
{gridpattern}
but in {ggplot2}
coerces aesthetics to a data frame and attempting to coerce grid::unit()
columns causes an error...New aesthetics to set different unit defaults
Build variant patterns in {gridpattern}
that use "cm" or "in" instead as default for pattern_spacing
, pattern_xoffset
, and pattern_yoffset
{gridpattern}
pattern functions are more flexible when used directly and could simply be a single "pattern" (before the {ggplot2}
coercion to a data frame makes things "hard")...Build a collection of variant patterns in a new package (perhaps called something like {gridpatternExtra}
) that aren't really "new" patterns but are variants that users could optionally bring in using the custom pattern extension mechanism to avoid issues with {ggplot2}
aesthetic limitations
Do nothing (let users manually figure out good "snpc" values or write their own custom pattern functions)
Seems to me like a new package of pattern variants may be the "best" "solution"
pattern_units
aesthetic so that pattern_spacing
is interpreted as centimeters or inches instead of the default "snpc" units.pattern_spacing
of 0.05 is almost surely too small if you aren't using the default "snpc" units...pattern_spacing
to a higher value):library(ggplot2)
library (ggpattern)
df <- data.frame (x=as.character (1:15),
y=rep (c(1, 3, -5), 5),
pattern = rep (c("a", "b", "b"), 5),
fill = rep (c("a", "b", "a"), 5),
facet = c(rep("x", 10), rep("y", 2), rep("z", 3)))
ggplot (df, aes (x=x, y=y, fill=fill, pattern = pattern)) +
geom_col_pattern (color = "black",
pattern_fill = "black",
pattern_angle = 45,
pattern_density = 0.1,
pattern_spacing = 0.20, pattern_units = "in",
pattern_key_scale_factor = 1.0) +
facet_grid (~facet, scales="free_x", space="free")
Bug description
Problems with pattern when combined with facet_grid (~facet, scales="free_x", space="free")
Minimal, reproducible example
Session info