7

On occasion, I have the need for some kind of pattern or textures for geom_bar() / geom_col() bars (i.e., for black-and-white printing). For example, the following can be hard for some people to view:

library(ggplot2)
library(dplyr, warn.conflicts=FALSE)
library(tidyr)

d <- iris %>% 
    group_by(Species) %>% 
    summarize_all(mean) %>% 
    gather(key, val, -Species)

ggplot(d, aes(x = Species, y = val, fill = key)) +
    geom_col(position = "dodge") +
    scale_fill_grey()

example bar chart

There have been good questions and answers on Stack Overflow (also here). However, the solutions are complicated and basically involve creating the patterns or textures by hand. I'm wondering if anyone has general ideas or suggestions or new approaches to solving this problem in a different way. Oftentimes when I think I can't do something with ggplot2, it means changing how I think about addressing it - but other (rare) times it just isn't implemented yet!

Joshua Rosenberg
  • 4,014
  • 9
  • 34
  • 73
  • 6
    IMHO any new approaches should be added to the (canonical) posts you linked to, and there's no real need to open a new question to address the same problem. If you want to try to attract new answers, you may consider placing a bounty over at those posts. Cheers. – Henrik Jan 28 '18 at 14:48
  • Does this answer your question? [How to add texture to fill colors in ggplot2](https://stackoverflow.com/questions/2895319/how-to-add-texture-to-fill-colors-in-ggplot2) – chemdork123 Jul 25 '20 at 02:24
  • 1
    Does this answer your question? [How can I add hatches, stripes or another pattern or texture to a barplot in ggplot?](https://stackoverflow.com/questions/62393159/how-can-i-add-hatches-stripes-or-another-pattern-or-texture-to-a-barplot-in-ggp) – Ian Campbell Nov 25 '20 at 22:15

1 Answers1

4

You can add patterns using the ggpattern package

# remotes::install_github("coolbutuseless/ggpattern")
library(ggpattern)
library(ggplot2)
library(dplyr, warn.conflicts=FALSE)
library(tidyr)
  
  d <- iris %>% 
    group_by(Species) %>% 
    summarize_all(mean) %>% 
    gather(key, val, -Species)
  
  ggplot(d, aes(x = Species, y = val, fill = key)) +
    geom_col_pattern(position = "dodge",
             pattern = 
               c(
                 "stripe", "stripe", "stripe", # 3rd col
                 "stripe", "stripe", "stripe", # 4th col
                 "none", "none", "none", # 1st col
                 "crosshatch", "crosshatch", "crosshatch" # 2nd col
             ),
             pattern_angle = c(rep(0, 3), 
                               rep(45, 3), 
                               rep(0, 6)),
             pattern_density = .1,
             pattern_spacing = .04,
             pattern_fill = 'black') +
    scale_fill_grey() +
  guides(fill = guide_legend(override.aes = 
                               list(
                                 pattern = c("none", "crosshatch", "stripe", "stripe"),
                                 pattern_spacing = .01,
                                 pattern_angle = c(0, 0, 0, 45)
                                 )
                             ))

Created on 2021-01-13 by the reprex package (v0.3.0)

John-Henry
  • 1,556
  • 8
  • 20