0

I have three variables which are factors: stimulus (12 levels), match (2 levels and Listgp (3 levels: T, TA and TQ). I already split the Listgp (Listener group) into three subset dataframes as follows.

mydata1T <-mydata[mydata$Listgp == "T", ]
mydata1TA <-mydata[mydata$Listgp == "TA", ]
mydata1TQ <-mydata[mydata$Listgp == "TQ", ]

Then I cross tabulated stimulusXmatch for each of the groups and even plotted them using barplot() and everything works just fine. However, when I try to use boxplot() in the lattice package, I get the error

(Error in boxplot.default(split(mf[[response]], mf[-response]), ...) 
 adding class "factor" to an invalid object).

The syntax I used is as follows.

par(mfrow=c(1,3))
boxplot(mydata1T$stimulus~mydata1T$match, data=mydata)
boxplot(mydata1TA$stimulus~mydata1TA$match, data=mydata)
boxplot(mydata1TA$stimulus~mydata1TQ$match, data=mydata)
par(mfrow=c(1,1))

How can I solve this problem and create three boxplots for the three group of listeners?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Shad
  • 63
  • 2
  • 11
  • I think the issue lies in the way you make your boxplots. In the first line, you have boxplot(data$variable ~data$othervariable, data=otherdata) and then you have boxplot(data$variable~otherdata$othervariable).... – Heroka Feb 10 '16 at 14:37
  • Can you update with a [sample data set](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610)? It's saying you're trying to plot a factor. Since factors have no percentiles, you can't plot them in a boxplot, though you can group by them like `boxplot(Sepal.Length ~ Species, data = iris)`. Variable order matters here. – alistaire Feb 10 '16 at 14:43
  • Thanks Heroka. I had a typo in the 3rd line boxplot(mydata1TA$stimulus~mydata1TQ$match, data=mydata) where the TA should be TQ, so it should read boxplot(mydata1TQ$stimulus~mydata1TQ$match, data=mydata). However, this is not the cause of the error with the boxplot. – Shad Feb 10 '16 at 15:09
  • Thanks alistaire. Even when I reverse the order of the two variables, I still get the same error (Error in boxplot.default(split(mf[[response]], mf[-response]), ...) : adding class "factor" to an invalid object ). In my next comment, I'll add some rows from the data. – Shad Feb 10 '16 at 15:12
  • I tried to paste some rows from my data set using the head () function but it's too big to be pasted here. – Shad Feb 10 '16 at 15:23
  • I saw in other posts in this platform that boxplot () does not work with any kind of factor. If this is the case, then it won't work in the case of my data either since all the three variables involved arr not numeric. – Shad Feb 10 '16 at 16:21
  • Shad, you are fine using a factor as long as the other variable is continuous. In other words, you can use a box plot to visualize the distribution of a variable across different levels of a categorical variable. You were trying to create a boxplot where both variables were categorical. That you cannot do. – Ryan Caldwell Feb 10 '16 at 16:35
  • Thanks Ryan. You are indeed right. However, all three variables in question are categorical. I think the only way to go around this is to first do xtabs() for each of the three Listgp levels and then do a boxplot of the xtabs. I tried this and it seems to work! – Shad Feb 10 '16 at 18:27

0 Answers0