0
votes

I'm having trouble setting the colors of my violin plots. I have a melted matrix, where one column is X (variable) group of each violin plot, and the second column is a values for these groups. (30x2. Please see an example of the data below.

I want to create a different color for each violin.

This is my data

    my_data=X1  50
            X1  30
            X1  40
            X1  30
            X1  40
            X1  33
            X1  45
            X1  54
            X1  34
            X1  54
            X2  77
            X2  80
            X2  70
            X2  60
            X2  55
            X2  77
            X2  79
            X2  80
            X2  82
            X2  83
            X3  10
            X3  12
            X3  14
            X3  15
            X3  12
            X3  12
            X3  11
            X3  9
            X3  8
            X3  10

This is my code:

library(ggplot2)
my_colors= c('#33adff' ,'#0066ff','#47d147')

ggplot(d, aes(x = variable, y =value , color=variable)) + 
geom_violin(trim = TRUE) + 
geom_jitter(height = 0, width = 0.1, color = "black") +

labs(y="A.U.") + 
xlab('') +
theme_classic()

How can set the each violin will have the colors that are set in the variable my colors?

I tried changing the fill to my_colors but it doesn't work. I also tried using scale_fill_manual but it doesn't change anything. Help will be much appreciated! Thanks

1

1 Answers

1
votes

colour and fill are not the same.

ggplot(d, aes(x = variable, y = value, fill = variable)) + 
  geom_violin(trim = TRUE) + 
  geom_jitter(height = 0, width = 0.1, color = "black") +
  scale_fill_manual(values = my_colors) +
  labs(y = "A.U.") + 
  xlab('') +
  theme_classic()