1

I would like to generate some violin plots using the vioplot library. Unfortunaly, vioplot takes vector as arguments. So it works as follow, but it's ugly:

library(vioplot)
x1 = rnorm(200, 10, 5)
x2 = rnorm(200, 20, 5)
x3 = rnorm(200, 30, 5)
x4 = rnorm(200, 10, 15)
x5 = rnorm(200, 10, 25)
x6 = rnorm(200, 20, 35)
x7 = rnorm(200, 40, 15)
x8 = rnorm(200, 50, 15)
// x9 and more .....

// plot 
vioplot(x1,x2,x3,x4,x5,x6,x7,x8)
// vioplot(x1 to x20 ) ? 

How can I improve it with a loop for example?

I would like to do something like :

  // Generate a random data frame
  df = data.frame()
  for (i in 1:100)
  {
      df = cbind(df, rnorm(200, 10, i))
  }

  // This line is not working
  vioplot(df[1:15],)

I think I need some meta programming to generate my argument list

DrIDK
  • 7,642
  • 2
  • 14
  • 14
  • 1
    It is always good to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). That makes it a lot easier for other to help you. – Jaap Apr 18 '17 at 12:20
  • have a look at `geom_violin` in `ggplot2` for an alternative – Richard Telford Apr 18 '17 at 12:21
  • geom_violin is working . But I would like to know if it was possible with vioplot by doing some meta programming or something else – DrIDK Apr 18 '17 at 12:30

1 Answers1

0

I succeed using meta programming as follow :

 arguments = list() 
 for (i in 1:15)
 {
   arguments[[i]] = rnorm(100, i) // positionnal arguments
 }

 arguments[["col"]] = "red" // named arguments 

 do.call("vioplot", arguments)
DrIDK
  • 7,642
  • 2
  • 14
  • 14