trevorld / ggpattern

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

[BUG] 'geom_col_pattern()' with numeric x values #93

Closed xlea23 closed 1 year ago

xlea23 commented 1 year ago

Bug description

Using ggpattern package in R, how can I not show a pattern for "NA" values but then have patterns continue after that point? When I run this code, there are no more patterns after an "NA" value even though there should be. There is a problem with ggpattern when it comes to repeating patterns.In the below code, you can see that fill acts appropriately, but pattern will never repeat twice, which causes the pattern from "S" to split over into the second red bar, which shouldn't have any pattern because it's "NA". See more information here.

Minimal, reproducible example

library(ggpattern)
library(tidyverse)

df <- data.frame(
  study_id = c(3, 3, 3, 3), primary_therapy = c("Si", "Si", "Si", "Si"),
  additional_therapy = c("NA", "S", "NA", "V+S"), end_yr = c(0.08, 0.39, 3.03, 3.4)) 

df %>%
  ggplot(aes(x = end_yr, y = study_id)) + 
  geom_col_pattern(aes(pattern = additional_therapy, 
                       fill = additional_therapy),
                   color = NA,
                   show.legend = TRUE,
                   position = "fill",
                   width = 3,
                   pattern_spacing = 0.01,
                   pattern_fill = "black",
                   pattern_color = NA,
                   pattern_size = 0.5,
                   pattern_density = 0.1,
                   pattern_linetype = 0.5,
                   pattern_orientation = "vertical") +
  scale_pattern_manual(
    name = "Additional Therapy",
    breaks = c("S", "V", "V+S"),
    values = c("S" = "stripe", "V" = "circle", "V+S" = "crosshatch", "NA" = "none"))
image

Session info

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

trevorld commented 1 year ago
library(ggplot2)
library(ggpattern)

df <- data.frame(trt = c("a", "b"),
                 x = c(0.08, 0.39),
                 y = rep(1, 2))

df %>%
  ggplot(aes(x = x, y = y)) +
  geom_col_pattern(aes(pattern = trt, fill = trt),
                   color = NA,
                   show.legend = TRUE,
                   position = "fill",
                   width = 3) +
  scale_pattern_manual(
    values = c("a" = "stripe", "b" = "circle", "c" = "none"))

ggp_bug

trevorld commented 1 year ago

Nota bene: @xlea23 if you just need to draw rectangles with numeric x and y axes then geom_rect_pattern() seems to currently be working correctly and (with a bit of work) should be able to produce the targeted chart...

trevorld commented 1 year ago
library(ggplot2)

df <- data.frame(
  study_id = c(3, 3, 3, 3), primary_therapy = c("Si", "Si", "Si", "Si"),
  additional_therapy = c("NA", "S", "NA", "V+S"), end_yr = c(0.08, 0.39, 3.03, 3.4))

df %>%
  ggplot(aes(x = end_yr, y = study_id)) +
  geom_col(aes(fill = additional_therapy),
           color = NA,
           show.legend = TRUE,
           position = "fill",
           width = 3,
           alpha = 0.5)

ggp_bug