Search code examples
rggplot2ggpattern

Why do the stripes only appear on legend and not on plot itself?


Trying to use ggpattern for this plot but can't get it to work right. Legend looks okay doesn't translate to what's on plot itself. Not stripes or dots on actual plot?

test <- tibble(names = c("fred", "harry", "tom"),
               start = c(1, 3, 5),
               end = c(10, 5, 7),
               stripe = c("yes", "no", "yes"))

ggplot() +
  geom_rect_pattern(data = test,
            aes(xmin = names,
                xmax = names,
                ymin = start,
                ymax = end,
                color = names,
                fill = names,
                pattern = stripe), size = 4)

enter image description here


Solution

  • To produce the plot without altering your data, you could try:

    ggplot() +
      geom_rect_pattern(data = test,
                        aes(xmin = as.numeric(factor(names)) - 0.25,
                            xmax = as.numeric(factor(names)) + 0.25,
                            ymin = start,
                            ymax = end,
                            fill = names,
                            pattern = stripe), pattern_fill = 'black', size = 0) +
      scale_x_continuous(breaks = seq(length(levels(factor(test$names)))),
                         labels = levels(factor(test$names))) +
      scale_pattern_manual(values = c('none', 'stripe'))
    

    enter image description here