title: How can I add hatches, stripes or another pattern or texture to a barplot in ggplot?
body: <p>Suppose I have data with both an ordinal variable and a categorical variable:</p>
  
  <pre><code>set.seed(35)
  df &lt;- data.frame(Class = factor(rep(c(1,2),times = 80), labels = c("Math","Science")),
                   StudyTime = factor(sort(sample(1:4, 16, prob = c(0.25,0.3,0.3,0.15), replace = TRUE)),labels = c("&lt;5","5-10","10-20","&gt;20")),
                   Nerd = factor(sapply(rep(c(0.1,0.3,0.5,0.8),c(30,50,50,30)), function(x)sample(c("Nerd","NotNerd"),size = 1, prob = c(x,1-x))),levels = c("NotNerd","Nerd")))
  </code></pre>
  
  <p>One could use <code>ggplot</code> and <code>geom_bar</code> with <code>x</code>, <code>fill</code> and <code>alpha</code> (or <code>color</code>) aesthetic mappings to visualize the relationship between these variables. </p>
  
  <pre><code>ggplot(data = df, aes(x = Class, fill = StudyTime, alpha = Nerd)) + 
    geom_bar(position = "dodge", color = "black") + 
    scale_alpha_manual(values = c(Nerd = 0.5, NotNerd = 1)) +
    scale_fill_manual(values = colorRampPalette(c("#0066CC","#FFFFFF","#FF8C00"))(4)) +
    labs(x = "Class", y = "Number of Students", alpha = "Nerd?") +
    theme(legend.key.height = unit(1, "cm"))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/SJrvQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/SJrvQ.png" alt="enter image description here"></a></p>
  
  <p>However, <code>alpha</code> and <code>color</code> are not ideal. A better alternative might be to apply a pattern such as stripes or a crosshatch. </p>
  
  <p>The accepted answer to <a href="https://stackoverflow.com/questions/2895319/how-to-add-texture-to-fill-colors-in-ggplot2">this question</a> from over 10 years ago says to use colors, and <a href="https://stackoverflow.com/a/20426482/13095326">the most upvoted answer</a> (while clever) uses over 100 lines of code. </p>
  
  <p><a href="https://stackoverflow.com/q/48487498/13095326">This question</a> received some upvotes but no new answers. </p>
  
  <p>Is there any better alternative to adding a pattern such as can be seen here?</p>
  
  <p><a href="https://i.stack.imgur.com/8G0C8.png" rel="noreferrer"><img src="https://i.stack.imgur.com/8G0C8.png" alt="enter image description here"></a></p>