Search code examples
rcolorsgroupggpattern

How can I change colors by using ggpattern?


I have a Design of 2 different Treatments (1)MIA: Poly_IC/Saline, 2) Ethanol:1/0) . I would like to assign different colors to Ethanol and a pattern to MIA. While the pattern worked out fine. I have trouble with the color.

Here is the code I used:

cort_v%>%
    ggplot(aes(x=Groups, y=x, fill=Groups, pattern=Treatment1, color=Treatment2)) +
   geom_boxplot()+
  geom_boxplot_pattern(position = position_dodge(preserve = "single"),
                       color = "black", 
                       pattern_fill = "black",
                       pattern_angle = 45,
                       pattern_density = 0.1,
                       pattern_spacing = 0.025,
                       pattern_key_scale_factor = 0.6) +
  scale_pattern_manual(values = c(1 = "stripe", 0 = "none")) +
  scale_color_manual(values = c("1" = "red", "0" = "white")) +
  geom_point()+
     theme_minimal()

In this code only the frame is colored and not filling the boxplot + it adds random colors Happy and grateful for any suggestions. Thanks a lot


Solution

  • You should use fill to specify Ethanol, not color. You're getting random colors because of fill = Groups.

    library(tidyverse)
    library(ggpattern)
    
    cort_v$Ethanol <- as.factor(cort_v$Ethanol)
    
    cort_v %>%
      ggplot(aes(x = Groups, y = nmol_L, fill = Ethanol, pattern = MIA)) +
      geom_boxplot() +
      geom_boxplot_pattern(position = position_dodge(preserve = "single"),
                           color = "black", 
                           pattern_fill = "black",
                           pattern_angle = 45,
                           pattern_density = 0.1,
                           pattern_spacing = 0.025,
                           pattern_key_scale_factor = 0.6) +
      scale_fill_manual(values = c("1" = "red", "0" = "white")) + # also scale_fill, not scale_color
      scale_pattern_manual(values = c(Poly_IC = "stripe", Saline = "none")) +
      geom_point() +
      theme_minimal()
    

    enter image description here

    UPDATE2: Since you're already using colors and patterns to signal variables, I would use separate() on the Groups variable and use facet_wrap() for gender -- female on the left, male on the right. To remove the pattern from the legend for Ethanol, you can add a override.aes argument to remove it.

    library(tidyverse)
    library(ggpattern)
    
    cort_v$Ethanol <- as.factor(cort_v$Ethanol)
    
    cort_v %>%
      separate(Groups, into = c("Gender", "NewGroup"), extra = "merge", sep = "_") %>%
      ggplot(aes(x = NewGroup, y = nmol_L, fill = Ethanol, pattern = MIA)) +
      geom_boxplot() +
      geom_boxplot_pattern(position = position_dodge(preserve = "single"),
                           color = "black", 
                           pattern_fill = "black",
                           pattern_angle = 45,
                           pattern_density = 0.1,
                           pattern_spacing = 0.025,
                           pattern_key_scale_factor = 0.6) +
      scale_fill_manual(values = c("1" = "red", "0" = "white")) + # also scale_fill, not scale_color
      scale_pattern_manual(values = c(Poly_IC = "stripe", Saline = "none")) +
      geom_point() +
      theme_minimal() + facet_wrap(~Gender) +
      guides(fill = guide_legend(override.aes = list(pattern = c("none", "none"))))
    

    enter image description here