0

I am trying to prepare a ggplot in r using the below data,

Product <- c(rep(c("Reference", "Test"), each = 500))
Value <- c(runif(min = 10, max = 100, n = 600),runif(min =20, max = 80, n = 400))
Method <- c(rep(c("Mehtod A", "Method B", "Method C", "Method D"), times = 250))
df <- data.frame(Product, Value, Method)

What I have already prepare looks like the below,

ggplot(df, aes(x=Product, y=Value)) + 
  geom_violin(aes(color= Product), 
              fill = "gray80", 
              size = 0.7, 
              alpha = .1, 
              trim = F, 
              na.rm = T)+
  geom_sina(aes(color= Product), 
            alpha = 0.3,  
            na.rm = T)+
  facet_grid(cols = vars(Method), 
             scales = "free")+
  geom_signif(test = "wilcox.test", 
              test.args = list(ref.group = "Reference"),
              comparisons = list(c("Test", "Reference")), 
              map_signif_level= function(p) sprintf("p = %.3g", p), 
              y_position = 130,
              na.rm = T)+
  labs(x="", 
       y="Value",
       title = toupper("Product Study"),
       subtitle = "Values per product for different methods",
       caption = "Data: Runif",
       tag = "Figure. 1")+
  scale_y_continuous(breaks = seq(0, 100, 20),
                     minor_breaks = seq(0, 100, 10))+
  guides(color = guide_legend(override.aes = list(size = 6)))+
  theme(axis.title.y = element_text(size=10, 
                                    face="bold"),
        axis.text.y = element_text(size=10, 
                                   face="bold"),
        axis.text.x = element_text(size=10, 
                                   face="bold"),
        plot.background = element_rect(fill = "white"),
        panel.background =  element_blank(),
        axis.line.x = element_line(color = "grey"),
        axis.line.y = element_line(color = "grey"),
        axis.ticks.y = element_line(color = "grey"),
        panel.border =  element_rect(fill = "NA", 
                                     colour = "darkgrey"),
        panel.grid.major = element_line(size = .5, 
                                        linetype = "solid"),
        panel.grid.minor = element_line(size = .25, 
                                        linetype = "solid"),
        panel.grid.major.x = element_line(color = "NA"),
        panel.grid.major.y = element_line(color = "grey"),
        panel.grid.minor.x = element_line(color = "NA"),
        panel.grid.minor.y = element_line(color = "grey90"),
        strip.background = element_rect(fill = "lightgrey",
                                        linetype = "solid",
                                        color = "darkgrey"),
        strip.text = element_text(size=10, 
                                  face="bold"))

but what I would like to do is to change the bellow theme element to something like,

strip.background = element_rect(fill = Method,
                                        linetype = "solid",
                                        color = "darkgrey")

so as to change the facets color based on the different Method (from df).

Unfortunately this doesn't work and sets the fill color to black for all of them!!!

Any help? Thanks

0 Answers0