trevorld / ggpattern

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

ggpattern layer reversing color scheme of choropleth #89

Closed mcunni23 closed 1 year ago

mcunni23 commented 1 year ago

Bug description

When I add a patterned layer to a ggplot choropleth, the color scheme of the choropleth is getting reversed.

Minimal, reproducible example

library(tigris)
library(sf)
library(RColorBrewer)
library(classInt)
library(tidyverse)
library(ggplot2)
library(ggpattern)

counties <- counties("Georgia")
leg <- state_legislative_districts("Georgia", "upper", cb = TRUE)

class <- classIntervals(counties$AWATER, 6, style="quantile")
counties <- mutate(counties, AWATER_cat = cut(AWATER, class$brks, include.lowest = TRUE))

counties_plot <- ggplot() + geom_sf(data = counties, aes(fill = AWATER_cat), col = 'black', show.legend = FALSE) +
  scale_fill_brewer(palette = "Greens")

class2 <- classIntervals(leg$ALAND, 2, style="quantile")
leg <- mutate(leg, ALAND_cat = cut(ALAND, class2$brks, include.lowest = TRUE))

test_plot <- counties_plot + geom_sf_pattern(data = leg, aes(fill = ALAND_cat, pattern = ALAND_cat), 
                                             color = "black", pattern_density = 0.025, pattern_color = 'blue',
                                             pattern_spacing = 0.025, alpha = 0, pattern_alpha = 0.75, show.legend = FALSE)

Session info

Please enter here the results of xfun::session_info("ggpattern")

R version 4.0.5 (2021-03-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 12.4, RStudio 2022.7.1.554

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  cachem_1.0.6       class_7.3.20       classInt_0.4.3    
  cli_3.3.0          colorspace_2.0.2   crayon_1.4.2      
  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_1.0.0    ggplot2_3.3.5     
  glue_1.6.2         graphics_4.0.5     grDevices_4.0.5   
  grid_4.0.5         gridpattern_1.0.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.2    
  MASS_7.3.55        Matrix_1.4.0       memoise_2.0.1     
  methods_4.0.5      mgcv_1.8.38        munsell_0.5.0     
  nlme_3.1.155       pillar_1.7.0       pkgconfig_2.0.3   
  png_0.1.7          proxy_0.4.26       R6_2.5.1          
  RColorBrewer_1.1.2 Rcpp_1.0.8         rlang_1.0.2       
  s2_1.0.7           scales_1.1.1       sf_1.0.5          
  splines_4.0.5      stats_4.0.5        tibble_3.1.7      
  tools_4.0.5        units_0.7.2        utf8_1.2.2        
  utils_4.0.5        vctrs_0.4.1        viridisLite_0.4.0 
  withr_2.4.3        wk_0.6.0          

MRE choropleth wo stripes MRE choropleth with stripes

trevorld commented 1 year ago

I'm observing that this color reversal also happens when the second layer is geom_sf() instead geom_sf_pattern():

counties_plot + geom_sf(data = leg, aes(fill = ALAND_cat), 
                                             color = "black", alpha = 0, show.legend = FALSE)

color_reverse

Closing since this doesn't seem to be a "bug" in {ggpattern}. Have you tried using scale_fill_manual() to manually fix the colors?