0

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

  • Welcome to StackOverflow. Can you make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and provide `dput(cort_v)`? – jrcalabrese Dec 06 '22 at 16:12
  • Thank you for your quick response. I added the dput – Hildi Schild Dec 06 '22 at 18:41

1 Answers1

0

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

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
  • Thank you so much!! Do you have an idea how to distinguish sex? eg, female boxplot transparent – Hildi Schild Dec 07 '22 at 11:28
  • See update: I would recommend using `facet_wrap` to specify sex. – jrcalabrese Dec 07 '22 at 13:00
  • If my post has answered your question, please click the green checkmark to accept it. – jrcalabrese Dec 08 '22 at 04:46
  • Again Thank you so much! The last thing I struggle with is the appearance of the legend. The pattern from MIA appears also in the Ethanol Label. But the Ethanol difference should be only color: red or white. – Hildi Schild Dec 08 '22 at 19:14
  • See update: You can use `guide_legend(override.aes)` to specify what kind of pattern (or none at all) should apply to the legend symbols. – jrcalabrese Dec 08 '22 at 23:05