trevorld / ggpattern

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

Patterns Don't Appear in Legend #124

Closed jvanbelzen closed 2 months ago

jvanbelzen commented 2 months ago

Bug description

Patterns appear in the graph, but not the legends. I noticed others described the same issue, but their problem seemed to be limited to exported image files. I have the issue in the image displayed in RStudio.

UPDATE The issue is specific to RStudio! If I run these same codes in R, the legend correctly displays the patterns.

Minimal, reproducible example

Two examples from independent posts (Stackoverflow and Posit). The responses indicate that the code produces the pattern in the graph and legends, as you would expect. However, when run on my end the same code produces the pattern in the graph but not the legend.

# Example 1
library(ggplot2)
library(ggpattern)
library(magrittr)

dat <- data.frame(drv = c("4", "4", "4", "4", "4", "f", "f", "f", "f", "r", "r", "r"), 
           class = c("compact", "midsize", "pickup", "subcompact", "suv", 
                     "compact", "midsize", "minivan", "subcompact", 
                     "2seater", "subcompact", "suv"), 
           y = c(12L, 3L, 33L, 4L, 51L, 35L, 38L, 11L, 22L, 5L, 9L, 11L)) 

dat 

# Give the same name for scale_fill_manual() and scale_pattern_manual(). Also, specify the group (or groups) you would like to show the pattern (or patterns).

dat %>% ggplot(aes(x = class, y = y, fill = drv, pattern = drv)) +
  #  geom_col()+   # not necessary
  geom_col_pattern() +
  coord_flip() +
  theme_minimal() +
  scale_fill_manual(
    name = "Drive",
    values = c(
      "4" = "#DA4943",
      "f" = "#D058EC",
      "r" = "#FF27D5"
    )
  ) +
  scale_pattern_manual(
    name = "Drive",
    values = c(
      "4" = "none",
      "f" = "stripe", 
      "r" = "none"
    )
  )

# Example 2
cData <- structure(list(Individual = c("Indv 1", "Indv 2", "Indv 3", "Indv 1", "Indv 2", "Indv 3", "Indv 1", "Indv 2", "Indv 3",
                                       "Indv 1", "Indv 2", "Indv 3", "Indv 1", "Indv 2", "Indv 3", "Indv 1", "Indv 2", "Indv 3"),
                        Change = c("Gain", "Gain", "Gain", "Gain", "Gain", "Gain", "Gain", "Gain", "Gain",
                                   "Loss", "Loss", "Loss", "Loss", "Loss", "Loss", "Loss", "Loss", "Loss"),
                        Type = c("Type A", "Type A", "Type A", "Type B", "Type B", "Type B", "Type C", "Type C", "Type C",
                                 "Type A", "Type A", "Type A", "Type B", "Type B", "Type B", "Type C", "Type C", "Type C"),
                        Value = c(39L, 5L, 12L, 28L, 21L, 7L, 21L, 9L, 116L, 28L, 108L, 30L, 0L, 3L, 25L, 13L, 4L, 26L),
                        Stripe_Angle = c(25L, 25L, 25L, 135L, 135L, 135L, 90L, 90L, 90L, 25L, 25L, 25L, 135L, 135L,
                                         135L, 90L, 90L, 90L)), row.names = c(NA, -18L), class = "data.frame")

library(ggplot2)
library(dplyr)
library(ggpattern)

colr <- c("orange", "steelblue", "darkred")

cData %>% 
  ggplot(aes(x=Change, y=Value), pattern="stripe") +
  geom_col_pattern(aes(pattern_angle=Type, color=Type, fill=Type),
                   pattern_fill="white", pattern_color="white", pattern_spacing=.05) +
  scale_color_manual(values = colr) +
  scale_fill_manual(values = colr) +
  scale_pattern_angle_manual(values = c(0, -45, 45)) +
  scale_y_continuous(breaks = seq(0, 140, 10)) +
  labs(x="", y="Amount") +
  facet_grid(~Individual, switch = "x") +
  theme_classic()

Example1 Example2

Session info

R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.4.1, RStudio 2023.12.1.402

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.8       class_7.3.22       classInt_0.4.10    cli_3.6.2          colorspace_2.1.0  
  DBI_1.2.2          e1071_1.7.14       fansi_1.0.6        farver_2.1.1       fastmap_1.1.1     
  ggpattern_1.0.1    ggplot2_3.5.1      glue_1.7.0         graphics_4.3.2     grDevices_4.3.2   
  grid_4.3.2         gridpattern_1.2.1  gtable_0.3.5       isoband_0.2.7      KernSmooth_2.23.22
  labeling_0.4.3     lattice_0.22.6     lifecycle_1.0.4    magrittr_2.0.3     MASS_7.3.60.0.1   
  Matrix_1.6.5       memoise_2.0.1      methods_4.3.2      mgcv_1.9.1         munsell_0.5.1     
  nlme_3.1.164       pillar_1.9.0       pkgconfig_2.0.3    png_0.1.8          proxy_0.4.27      
  R6_2.5.1           RColorBrewer_1.1.3 Rcpp_1.0.12        rlang_1.1.3        s2_1.1.6          
  scales_1.3.0       sf_1.0.16          splines_4.3.2      stats_4.3.2        tibble_3.2.1      
  tools_4.3.2        units_0.8.5        utf8_1.2.4         utils_4.3.2        vctrs_0.6.5       
  viridisLite_0.4.2  withr_3.0.0        wk_0.9.1          
trevorld commented 2 months ago

When I run the first example in my terminal I get:

Screenshot from 2024-05-09 14-37-38

and the second example looks like:

Screenshot from 2024-05-09 14-38-13

What graphic device are you drawing with? What does names(dev.cur()) give you?

trevorld commented 2 months ago

This is a duplicate of #86 which remains open. I still need a minimal, reproducible example...

jvanbelzen commented 2 months ago

What graphic device are you drawing with? What does names(dev.cur()) give you?

[1] "RStudioGD"

And it does only seem to be an issue for me when working in RStudio. Is ggpatterns() not compatible with RStudio?

trevorld commented 2 months ago
trevorld commented 1 month ago

And it does only seem to be an issue for me when working in RStudio. Is {ggpatterns} not compatible with RStudio?

I just installed RStudio on my Linux laptop and the charts work fine for me in RStudio.

I wildly speculate that maybe it is defaulting to the "quartz" device and maybe there is something weird going on the with the quartz png device which the "RStudioGD" device maybe wrapping around that doesn't affect the cairo png device?

You may want to click on "Tools" -> "Global Options" -> "Graphics" and try different graphics backends...