trevorld / ggpattern

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

Example doesn't work with namespacing #40

Closed russHyde closed 3 years ago

russHyde commented 3 years ago

I'm trying to reproduce the ggpattern example from the README. I'm using namespacing to identify the relevant functions.

In a new R session:

  df = data.frame(
    level = c("a", "b", "c", "d"),
    outcome = c(2.3, 1.9, 3.2, 1)
  )

  ggplot2::ggplot(df) +
    ggpattern::geom_col_pattern(
      ggplot2::aes(level, outcome, pattern_fill = level),
      pattern = "stripe",
      fill    = "white",
      colour  = "black"
    ) +
    ggplot2::theme_bw(18) +
    ggplot2::theme(legend.position = "none") +
    ggplot2::labs(
      title = "ggpattern::geom_pattern_col()",
      subtitle = "pattern = 'stripe'"
    ) +
    ggplot2::coord_fixed(ratio = 1 / 2)

This throws "Error: Unknown colour name: a" (and doesn't make the plot)


On loading ggplot2 / ggpattern first:

library(ggplot2)
# Need help? Try Stackoverflow: https://stackoverflow.com/tags/ggplot2
library(ggpattern)

# Attaching package: ‘ggpattern’

# The following objects are masked from ‘package:ggplot2’:

#     flip_data, flipped_names, gg_dep, has_flipped_aes, remove_missing, should_stop, waiver

df = data.frame(
    level = c("a", "b", "c", "d"),
    outcome = c(2.3, 1.9, 3.2, 1)
  )

  ggplot2::ggplot(df) +
    ggpattern::geom_col_pattern(
      ggplot2::aes(level, outcome, pattern_fill = level),
      pattern = "stripe",
      fill    = "white",
      colour  = "black"
    ) +
    ggplot2::theme_bw(18) +
    ggplot2::theme(legend.position = "none") +
    ggplot2::labs(
      title = "ggpattern::geom_pattern_col()",
      subtitle = "pattern = 'stripe'"
    ) +
    ggplot2::coord_fixed(ratio = 1 / 2)

The same code works without error (and makes the plot)

I expected that name::spaced() use of the ggpattern functions would generate the plot without error. Is there some issue with exporting?

russHyde commented 3 years ago
sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8    LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggpattern_0.1.3 ggplot2_3.3.3  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6         pillar_1.6.1       compiler_4.1.0     class_7.3-19       tools_4.1.0       
 [6] digest_0.6.27      lifecycle_1.0.0    tibble_3.1.2       gtable_0.3.0       pkgconfig_2.0.3   
[11] png_0.1-7          rlang_0.4.11       DBI_1.1.1          magick_2.7.2       e1071_1.7-7       
[16] withr_2.4.2        dplyr_1.0.6        styler_1.4.1       generics_0.1.0     vctrs_0.3.8       
[21] classInt_0.4-3     grid_4.1.0         tidyselect_1.1.1   glue_1.4.2         sf_1.0-0          
[26] R6_2.5.0           fansi_0.5.0        polyclip_1.10-0    farver_2.1.0       purrr_0.3.4       
[31] magrittr_2.0.1     backports_1.2.1    scales_1.1.1       ellipsis_0.3.2     units_0.7-2       
[36] assertthat_0.2.1   colorspace_2.0-1   labeling_0.4.2     utf8_1.2.1         KernSmooth_2.23-20
[41] proxy_0.4-26       gridGeometry_0.2-0 munsell_0.5.0      crayon_1.4.1
trevorld commented 3 years ago

I can reproduce the "bug". There is no ERROR if just library(ggpattern) is in preamble (no need to library(ggplot2)) is used or if you explicitly indicate the pattern_fill colours (outside the aes()):

  df = data.frame(
    level = c("a", "b", "c", "d"),
    outcome = c(2.3, 1.9, 3.2, 1)
  )

  ggplot2::ggplot(df) +
    ggpattern::geom_col_pattern(
      ggplot2::aes(level, outcome),
      pattern = "stripe",
      pattern_fill = c("yellow", "blue", "red", "green"),
      fill    = "white",
      colour  = "black"
    ) +
    ggplot2::theme_bw(18) +
    ggplot2::theme(legend.position = "none") +
    ggplot2::labs(
      title = "ggpattern::geom_pattern_col()",
      subtitle = "pattern = 'stripe'"
    ) +
    ggplot2::coord_fixed(ratio = 1 / 2)
russHyde commented 3 years ago

The intended use for the code, was inside a package. So no library calls would be in the preamble.

trevorld commented 3 years ago

Besides explicitly indicating the colors with pattern_fill outside the aes() (perhaps using a color palette) the other alternative that works right now without importing all of {ggpattern} is to explicitly add a member of the the scale_pattern_fill_*() family like ggpattern::scale_pattern_fill_discrete() or perhaps another discrete pattern fill alternative like ggpattern::scale_pattern_fill_brewer() to your call i.e.

  df = data.frame(
    level = c("a", "b", "c", "d"),
    outcome = c(2.3, 1.9, 3.2, 1)
  )

  ggplot2::ggplot(df) +
    ggpattern::geom_col_pattern(
      ggplot2::aes(level, outcome, pattern_fill = level),
      pattern = "stripe",
      fill    = "white",
      colour  = "black"
    ) +
    ggplot2::theme_bw(18) +
    ggplot2::theme(legend.position = "none") +
    ggplot2::labs(
      title = "ggpattern::geom_pattern_col()",
      subtitle = "pattern = 'stripe'"
    ) +
    ggplot2::coord_fixed(ratio = 1 / 2) +
    ggpattern::scale_pattern_fill_discrete()
trevorld commented 3 years ago

I think {ggplot2} must be able to see {scale_pattern_fill_discrete()} in an environment/namespace it can access in order to auto-magically dispatch it if you don't explicitly add ggpattern::scale_pattern_fill_discrete() to your plot statement. So I think you would need to explicitly import it into your package NAMESPACE for auto-magical dispatch if you don't want to explicitly add it to your statement. The fact that {ggplot2} can't find scale_pattern_fill_discrete() when it isn't available in a shared environment doesn't seem like a bug on the part of {ggpattern}.

russHyde commented 3 years ago

THanks