Search code examples
rggplot2violin-plotggpattern

Plot violin plot with multiple pattern


I want a violin plot with 4 violin, 2 with striped pattern and 2 with no pattern.

I only obtain striped pattern, can you help me please?

 gg <- ggplot(AdPluslong, aes(Eval, Total.Score)) +
       geom_violin_pattern(aes(fill = as.factor(TypeGroupe),
                               pattern_shape = as.factor(TypeGroupe)),
         pattern = 'stripe',
         pattern_density = 0.001,
         pattern_angle = 45,
         colour  = 'black'
       )+scale_fill_manual(values=c("#E69F00", "#56B4E9","#E69F00", "#56B4E9"))

Solution

  • It's difficult to know exactly what you are looking for without having your data or a sketch of the desired output (there are 6 different ways that two out of four violins could be striped), but using the pattern alpha seems like the easiest solution to me here:

    ggplot(AdPluslong, aes(Eval, Total.Score)) +
      geom_violin_pattern(aes(fill = as.factor(TypeGroupe),
                              pattern_alpha = as.factor(TypeGroupe)),
                          pattern_density = 0.001,
                          pattern_angle = 45,
                          colour  = 'black') + 
      scale_fill_manual(values = c("#E69F00", "#56B4E9")) +
      scale_pattern_alpha_manual(values = c(1, 0))
    

    enter image description here


    Data used

    There was no data included in the question, so I have inferred its approximate properties as follows:

    library(ggplot2)
    library(ggpattern)
    
    set.seed(1)
    
    AdPluslong <- data.frame(Eval = rep(c('A', 'B'), 100),
                             Total.Score = rnorm(200),
                             TypeGroupe = rep(1:2, each = 100))