0

I want to make a graph with four violin plots of four different groups. I am getting an error and I do not know what to fix. Each group (clusters) has two columns, a group identifier and age values (in months). This is the code that I tried to run.

library(vioplot) 
vioplot(cluster1, cluster2, cluster3, clusterTD, names = c("Group 1","Group 2","Group 3","Group 4 (TD)"), col=(c("hotpink", "darkorange1", "mediumpurple1", "blue")))
title("Violin Plots of Age in Months"))

This is the error that I got.

Error in [.data.frame(x, order(x, na.last = na.last, decreasing = decreasing)) : undefined columns selected

Does anyone know what I am doing wrong?

How do I interpret what the error message?

Is this because x and y are not defined clearly?

I want the y axis to be the Age values and the x to be the group (clusters), so that there are four groups.

I really appreciate any help that I receive!!

  • 2
    Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). What are `cluster1`, etc? what is `class(cluster1)` and so on? – mathematical.coffee Jul 23 '15 at 00:15
  • I suspect that you should pass in `cluster1$Age` (or whatever the y column is called) rather than the entire `cluster1` and the same for the other clusters. – mathematical.coffee Jul 23 '15 at 00:23
  • each cluster is a different group of individuals which I want to compare. – michele amato Jul 23 '15 at 23:02
  • that's not enough to help you with, please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). By "what is `cluster1`" I mean in R, not in the context of your analysis - a data frame? list? vector? etc. We cannot debug your problem at the moment because we cannot replicate it. – mathematical.coffee Jul 24 '15 at 00:31
  • The cluster1$Age solution solved my problem!!! You are amazing thanks for your help!!! :] – michele amato Jul 24 '15 at 00:33

1 Answers1

0

Without a reproducible example hard to give you the exact solution. I guess that one or more of your class is a data.frame. This reproduce the error:

library(vioplot)
x1 <- mtcars$mpg[mtcars$cyl==4] 
vioplot(data.frame(x1),names=c("4 cy"), col="gold") 

You should unlist your clusters arguments.

vioplot(unlist(cluster1), unlist(cluster2), unlist(cluster3),...)

the error:

Internally violplot is calling quantile S3method. This statement reproduce the same error:

quantile(data.frame(x1),0.25)

In fact, the default quantile S3method is called and it is expecting a numeric vector. It fails with a data.frame. I think that a check in the begining of the function should be added to the quantile default function. Something like :

if(!is.vector(x)) stop("arg should be a numeric vector")
agstudy
  • 119,832
  • 17
  • 199
  • 261