how can I create violin plot in different colours?

r

Solution

It is not possible to have many colors. But it is not difficult to hack the function `vioplot` and edit the source code. Here steps you should follow to accomplish this:

copy the initial function:

 my.vioplot <- vioplot()

edit this function:

 edit(my.vioplot)

Search the word "polygon" and and replace col by col[i]

Do a test in the beginning of function for the case you give a single color. and add this line :

 if(length(col)==1) col <- rep(col,n)

For example using your data :

vioplot(x1, x2, x3, names=c("4 cyl", "6 cyl", "8 cyl"), col="gold") 
title("Violin Plots of Miles Per Gallon") 

my.vioplot(x1, x2, x3, names=c("4 cyl", "6 cyl", "8 cyl"), col=c("gold","red","blue")) 
title("Violin Plots of Miles Per Gallon multi colors") 

Problem

I am using package `vioplot`. I would like to ask, how can I create violinplot in different colours. This is my reproducible example: ``` # Violin Plots library(vioplot) x1 <- mtcars$mpg[mtcars$cyl==4] x2 <- mtcars$mpg[mtcars$cyl==6] x3 <- mtcars$mpg[mtcars$cyl==8] vioplot(x1, x2, x3, names=c("4 cyl", "6 cyl", "8 cyl"), col="gold") title("Violin Plots of Miles Per Gallon") ``` Thank you.

Original source