Question: R multiple parameters
0
gravatar for sacha
2.0 years ago by
sacha1.7k
France
sacha1.7k wrote:

I m using vioplot R library to generate violon plot. How can I avoid the following stupid code with a loop or something else. ( the number of arguments to use should be random. In the following code it's 15 )

otu = otu_table(varBiom)
x1 = as.vector(otu[1,])
x2 = as.vector(otu[2,])
x3 = as.vector(otu[3,])
x4 = as.vector(otu[4,])
x5 = as.vector(otu[5,])
x6 = as.vector(otu[6,])
x7 = as.vector(otu[7,])
x8 = as.vector(otu[8,])
x9 = as.vector(otu[9,])
x10 = as.vector(otu[10,])
x11 = as.vector(otu[11,])
x12 = as.vector(otu[12,])
x13 = as.vector(otu[13,])
x14 = as.vector(otu[14,])
x15 = as.vector(otu[15,])

vioplot(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15)
code parameter R • 768 views
ADD COMMENTlink modified 2.0 years ago • written 2.0 years ago by sacha1.7k
1

What is your final plot like? Why are you passing 15 vectors to vioplot? Can't you pass individual vectors and use lapply / sapply, something like

sapply(otu, vioplot)

And most importantly, what is the output of str(otu)

ADD REPLYlink written 2.0 years ago by Santosh Anand4.7k

Or an apply function for the call to as.vector().

ADD REPLYlink written 2.0 years ago by WouterDeCoster38k

I would like the following plot : https://framapic.org/h304KZZBwDGG/Y75ElIRvkvze.png each x[1..15] is a vector of numeric value .

vioplot takes as argument x and "..." for additional data vectors.

ADD REPLYlink written 2.0 years ago by sacha1.7k

This is a limitation of the way vioplot is written. I remember years ago editing its source to accept a list of vectors. Sorry I don't have it to share, I just remember there was no function you're looking for. In plain R you could have multiple single plots with par(mfrow) and a loop. Because it accepts the ..., editing vioplot was not tricky, and the source is available.

ADD REPLYlink written 2.0 years ago by karl.stamm3.4k
3
gravatar for Ido Tamir
2.0 years ago by
Ido Tamir5.0k
Austria
Ido Tamir5.0k wrote:

better ask on generic R questions on stackoverflow or the R mailing list and include a simply reproducible input. I would switch to ggplot2 where you plot from a "long" data frame - each observation in its own row.

library("reshape2")
library("ggplot2")
library("phyloseq")

data(GlobalPatterns)
otu <- as.data.frame(otu_table(GlobalPatterns))[1:15,]
otu$taxon <- rownames(otu)
otu.m <- melt(otu, id.vars="taxon")
ggplot(otu.m, aes(x=taxon,y=value)) + geom_violin()
ADD COMMENTlink written 2.0 years ago by Ido Tamir5.0k

Thanks ! It's not so pretty than vioplot , but I would be able to customize it I guess... Even if ggplot2 looks black magic for me :D

ADD REPLYlink written 2.0 years ago by sacha1.7k
2
gravatar for sacha
2.0 years ago by
sacha1.7k
France
sacha1.7k wrote:

I think I found a way with meta programming. The following line works :

   otu = otu_table(varBiom)
   arguments= list()
   for (i in 1:15)
   {
     arguments[[i]] = as.vector(otu[i,])
   }

  do.call("vioplot", arguments)
ADD COMMENTlink modified 2.0 years ago • written 2.0 years ago by sacha1.7k

Oh that's really nice!

ADD REPLYlink written 2.0 years ago by karl.stamm3.4k

I renamed to clarify.

arguments[[NUMBER]] are positionnal arguments : function(a,b,c .. ) arguments[[STRING]] are named arguments : function(color="red")

ADD REPLYlink written 2.0 years ago by sacha1.7k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1052 users visited in the last hour