rggplot2ggpattern

Cross hatches with ggplot2


I'm trying to make a bar plot, and for the treatments that include insecticide, I want there to be cross hatches, but the code I currently won't work and I cannot figure out how to fix it.

Here is a subset of my data:

dput(peak.hillmeans1[1:12,])
structure(list(Subplot_Descriptions = c("ambient", "ambient", 
"ambient", "ambient", "drought", "drought", "drought", "drought", 
"drought_insecticide", "drought_insecticide", "drought_insecticide", 
"drought_insecticide"), hill.number.type = c("H0", "H1", "H2", 
"Hinf", "H0", "H1", "H2", "Hinf", "H0", "H1", "H2", "Hinf"), 
    hill.number.mean = c(203.425, 41.2441606562857, 12.737168631955, 
    4.18742498977372, 243.933333333333, 47.6826726791051, 14.0547025275759, 
    4.50664860697016, 259.016666666667, 47.6003166273996, 13.7637529655749, 
    4.55027488751543), hill.number.sd = c(60.6538518974682, 6.51934912505994, 
    1.47899803604672, 0.379117399794485, 56.4631708874614, 9.62482790860152, 
    2.24482412930567, 0.537764198394058, 48.9080225184649, 8.0756230742672, 
    1.26719037679015, 0.478045892509355), insecticide = c("ambient_i", 
    "ambient_i", "ambient_i", "ambient_i", "ambient_i", "ambient_i", 
    "ambient_i", "ambient_i", "insecticide", "insecticide", "insecticide", 
    "insecticide")), row.names = c(NA, 12L), class = "data.frame")

This is the code I have now:

library(ggplot2)
library(ggpattern)

ggplot(peak.hillmeans1, aes(x = hill.number.type, 
                            y = hill.number.mean, 
                            fill = Subplot_Descriptions)) + 
  geom_bar(stat = "identity", position = position_dodge()) +
  geom_errorbar(aes(ymin = hill.number.mean - hill.number.sd, 
                    ymax = hill.number.mean + hill.number.sd), 
                width = 0.2, position = position_dodge(0.9)) + 
  geom_bar_pattern(aes(pattern = insecticide),
                   position = position_dodge(preserve = "single"),
                   pattern = "stripe",
                   pattern_density = 0.1,
                   pattern_spacing = 0.025,
                   pattern_key_scale_factor = 0.6,
                   color = "black") +
  scale_pattern_manual(values = c(insecticide = "stripe", ambient_i = "none")) +
  scale_fill_manual(values = c("ambient" = "darkblue",
                               "drought" = "gray58",
                               "warmed" = "red2",
                               "warmed_drought" = "darkred",
                               "drought_insecticide" = "dodgerblue4", 
                               "insecticide" = "sienna1", 
                               'warmed_drought_insecticide' = "orangered", 
                               'warmed_insecticide' = "salmon")) +
  labs(title = "Mean Hill Numbers for Peak Drought", 
       x = "Hill Number", y = "Mean # of Species", fill = "Treatment") +
  theme_minimal()

Solution

  • library(ggplot2)
    library(ggpattern)
    
    ggplot(peak.hillmeans1, aes(x = hill.number.type, 
                                y = hill.number.mean, 
                                fill = Subplot_Descriptions)) + 
      geom_bar_pattern(aes(pattern = insecticide),
                       stat = "identity",
                       position = position_dodge(preserve = "single"),
                       pattern_density = 0.1,
                       pattern_spacing = 0.025,
                       pattern_key_scale_factor = 0.6,
                       color = "black") +
      geom_errorbar(aes(ymin = hill.number.mean - hill.number.sd, 
                        ymax = hill.number.mean + hill.number.sd), 
                    width = 0.2, position = position_dodge(0.9)) + 
      scale_pattern_manual(values=c(insecticide = 'stripe', ambient_i = 'none'),
                           guide = guide_legend(override.aes=list(fill=NA))) +
      scale_fill_manual(values = c("ambient" = "darkblue",
                                   "drought" = "gray58",
                                   "warmed" = "red2",
                                   "warmed_drought" = "darkred",
                                   "drought_insecticide" = "dodgerblue4", 
                                   "insecticide" = "sienna1", 
                                   'warmed_drought_insecticide' = "orangered", 
                                   'warmed_insecticide' = "salmon"),
                        guide = guide_legend(override.aes = list(pattern = "none"))) +
      labs(title = "Mean Hill Numbers for Peak Drought", 
           x = "Hill Number", y = "Mean # of Species", 
           fill = "Treatment", pattern = "Insecticide") +
      theme_minimal() 
    

    Created on 2024-04-17 with reprex v2.0.2