trevorld / ggpattern

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

Magick patterns are distorted in the legend #63

Closed dansmith01 closed 2 years ago

dansmith01 commented 2 years ago

Bug description

The magick package's built-in patterns look great in the plot itself, but are distorted in the legend.

I realize you've noted this in the README's "Limitations" section as "Legend rendering for patterns is still not great. Use pattern_key_scale_factor to adjust legend appearance." I wanted to bring it up as an issue to see a fix can be found. The suggested workaround of using pattern_key_scale_factor doesn't seem to make it any difference, other than increasing the rendering time.

I'll start looking into a fix myself and submit a PR if successful; any kind of direction you can give would be appreciated.

library(ggplot2)
library(ggpattern)

ggplot(data.frame(x=LETTERS[1:3], y=1:3)) + 
geom_bar_pattern(aes(x=x, y=y, pattern_type=x), color="black", fill="white", pattern="magick", stat="identity") + 
scale_pattern_type_discrete(choices = c('bricks', 'fishscales', 'right45')) + 
theme(legend.key.size = unit(4, "lines"))

Rplot

Session info All packages are current as of this posting - 12/9/2021

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

Locale:
  LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
  LC_NUMERIC=C                           LC_TIME=English_United States.1252    

Package version:
  cachem_1.0.6        class_7.3.19        classInt_0.4.3      cli_3.1.0           colorspace_2.0.2    cpp11_0.4.2        
  crayon_1.4.2        DBI_1.1.1           digest_0.6.29       e1071_1.7.9         ellipsis_0.3.2      fansi_0.5.0        
  farver_2.1.0        fastmap_1.1.0       ggpattern_0.3.2-1   ggplot2_3.3.5       glue_1.5.1          graphics_4.1.2     
  grDevices_4.1.2     grid_4.1.2          gridpattern_0.3.2.1 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.1      MASS_7.3.54         Matrix_1.3.4       
  memoise_2.0.1       methods_4.1.2       mgcv_1.8.38         munsell_0.5.0       nlme_3.1.153        pillar_1.6.4       
  pkgconfig_2.0.3     png_0.1.7           proxy_0.4.26        R6_2.5.1            RColorBrewer_1.1.2  Rcpp_1.0.7         
  rlang_0.4.12        s2_1.0.7            scales_1.1.1        sf_1.0.4            splines_4.1.2       stats_4.1.2        
  tibble_3.1.6        tools_4.1.2         units_0.7.2         utf8_1.2.2          utils_4.1.2         vctrs_0.3.8        
  viridisLite_0.4.0   withr_2.4.3         wk_0.5.0           
trevorld commented 2 years ago

In {ggpattern} the default resolutions for the main pattern and the legend key are different but you can override the default resolution for both with ggpattern_res global option. Doing so seems to work better for the "magick" patttern. For example your same chart with options(ggpattern_res = 60) (set default resolution of 60 pixels per inch):

Rplot001

dansmith01 commented 2 years ago

Thanks @trevorld!

After poking through the code, I noticed setting pattern_res worked too.

ggplot(data.frame(x=LETTERS[1:3], y=1:3)) + 
geom_bar_pattern(aes(x=x, y=y, pattern_type=x), color="black", fill="white", pattern="magick", stat="identity", pattern_res=72) + 
scale_pattern_type_discrete(choices = c('bricks', 'fishscales', 'right45')) + 
theme(legend.key.size = unit(4, "lines"))

Rplot