rggplot2ggpattern

adding pattern to a barplot


I am having issues making only one of my grouped bars in my bar plot to have a pattern. I just want the middle bar (white one) to have a pattern. I also want all of them to have black outlines. Right now the code makes all three of them have patterns.

#load libraries

library(ggplot2)
library(ggpattern)

#create a dataset

specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie,condition,value)

data

#create plot

#right now this makes all bars have pattern. I only want the middle one to have a pattern and I want all of the bars to have black outlines

ggplot(data, aes(fill=condition, y=value, x=specie)) + 
  geom_bar(position="dodge", stat="identity")+ theme_classic() +
  theme(text=element_text(size=16,  family="Sans"))+ labs(x='Team', y='Points', title='Avg. Points Scored by Position & Team') +
  theme(plot.title = element_text(hjust=0.5, size=20, face='bold')) +
  scale_fill_manual('Position', values=c('black', 'white', 'lightgrey')) +
  geom_col_pattern(position = "dodge",
                   pattern = "stripe",
                   pattern_angle = 45,
                   pattern_density = .1,
                   pattern_spacing = .04,
                   pattern_fill = 'black') +
  guides(fill = guide_legend(override.aes = 
                               list(
                                 pattern = c("none", "stripe", "none"),
                                 pattern_spacing = .01,
                                 pattern_angle = c(0, 45, 0)
                               )
  ))



Solution

  • One option to achieve your desired result would be to map condition on the pattern aes, then set your desired patterns via scale_pattern_manual, i.e. set it to "none" for categories for which you don't want a pattern. And for your outline set color="black". Also note that I dropped the geom_bar.

    library(ggplot2)
    library(ggpattern)
    
    set.seed(123)
    
    ggplot(data, aes(fill = condition, y = value, x = specie)) +
      scale_fill_manual(
        "Position",
        values = c("black", "white", "lightgrey")
      ) +
      scale_pattern_manual(
        "Position",
        values = c("none", "stripe", "none")
      ) +
      geom_col_pattern(
        aes(pattern = condition),
        position = "dodge",
        pattern_angle = 45,
        pattern_density = .1,
        pattern_spacing = .04,
        pattern_fill = "black",
        color = "black"
      ) +
      theme_classic() +
      theme(
        text = element_text(size = 16, family = "sans"),
        plot.title = element_text(hjust = 0.5, size = 20, face = "bold")
      ) +
      labs(
        x = "Team", y = "Points",
        title = "Avg. Points Scored by Position & Team"
      )