trevorld / ggpattern

ggplot geoms with pattern fills
https://trevorldavis.com/R/ggpattern/dev/
Other
356 stars 18 forks source link

'facet_grid(space="free")' with aesthetics defined in 'snpc' units #81

Closed ncrnalab closed 2 months ago

ncrnalab commented 2 years ago

Bug description

Problems with pattern when combined with facet_grid (~facet, scales="free_x", space="free")

Minimal, reproducible example

# R code for minimal, reproducible example goes here

library (tidyverse)
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.01,
                    pattern_key_scale_factor = 0.6) +
  facet_grid (~facet, scales="free_x", space="free")

Session info

R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044), RStudio 2022.2.0.443

Locale:
  LC_COLLATE=English_Denmark.1252  LC_CTYPE=English_Denmark.1252    LC_MONETARY=English_Denmark.1252
  LC_NUMERIC=C                     LC_TIME=English_Denmark.1252    

Package version:
  cachem_1.0.6       class_7.3.20       classInt_0.4.3     cli_3.2.0          colorspace_2.0.3  
  crayon_1.5.1       DBI_1.1.2          digest_0.6.29      e1071_1.7.9        ellipsis_0.3.2    
  fansi_1.0.2        farver_2.1.0       fastmap_1.1.0      ggpattern_0.4.2    ggplot2_3.3.6     
  glue_1.6.2         graphics_4.1.2     grDevices_4.1.2    grid_4.1.2         gridpattern_0.5.3 
  gtable_0.3.0       isoband_0.2.5      KernSmooth_2.23.20 labeling_0.4.2     lattice_0.20.45   
  lifecycle_1.0.1    magrittr_2.0.2     MASS_7.3.57        Matrix_1.4.1       memoise_2.0.1     
  methods_4.1.2      mgcv_1.8.40        munsell_0.5.0      nlme_3.1.157       pillar_1.7.0      
  pkgconfig_2.0.3    png_0.1.7          proxy_0.4.26       R6_2.5.1           RColorBrewer_1.1.3
  Rcpp_1.0.8.3       rlang_1.0.1        s2_1.0.7           scales_1.2.0       sf_1.0.7          
  splines_4.1.2      stats_4.1.2        tibble_3.1.6       tools_4.1.2        units_0.8.0       
  utf8_1.2.2         utils_4.1.2        vctrs_0.3.8        viridisLite_0.4.0  withr_2.5.0       
  wk_0.6.0 

Rplot

trevorld commented 2 years ago
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"))

free_space

trevorld commented 2 years ago

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"))

cm_pattern

trevorld commented 2 years ago

Possible "solutions" for when facet_grid(space="free"):


Seems to me like a new package of pattern variants may be the "best" "solution"

trevorld commented 2 months ago
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")

image