-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'facet_grid(space="free")' with aesthetics defined in 'snpc' units #81
Comments
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
Seems to me like a new package of pattern variants may be the "best" "solution" |
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")
|
* {ggpattern} now supports the `pattern_units` aesthetic (#81). Supported by most "geometry" patterns. It sets the `grid::unit()` used by the `pattern_spacing`, `pattern_xoffset`, `pattern_yoffset`, and (for the "wave" pattern) the `pattern_frequency` aesthetics. Default is "snpc" while "cm" and "inches" are likely alternatives. closes #81
* {ggpattern} now supports the `pattern_units` aesthetic (#81). Supported by most "geometry" patterns. It sets the `grid::unit()` used by the `pattern_spacing`, `pattern_xoffset`, `pattern_yoffset`, and (for the "wave" pattern) the `pattern_frequency` aesthetics. Default is "snpc" while "cm" and "inches" are likely alternatives. closes #81
Bug description
Problems with pattern when combined with facet_grid (~facet, scales="free_x", space="free")
Minimal, reproducible example
Session info
The text was updated successfully, but these errors were encountered: