0

I need to use the drop.levels function in my box and whisker plot. The data I'm working with is Dance, the data frames are dance$new and dance$type, the variables I want included are Contra, Blues, and Swing. There are 3 other variables I do not want included; Lindy, Salsa, and Tango.

This is what I have:

box.labels<-c("Blues","Contra","Swing")
boxplot(dance$new~dance$type, ylab="Dance Count",
xlab="Type", name=box.labels, drop.levels(Lindy, Salsa, Tango),
main="Dancing for a Healthier You")

Am I incorporating drop.levels wrong?

Vixxen81
  • 33
  • 1
  • 7
  • 1
    You should add a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for this. We don't know what your data looks like. – LyzandeR Nov 24 '15 at 16:22
  • Plus you have not upvoted or accepted or even commented on any answers so far which is considered rude to say the least. Please also read the [tour](http://stackoverflow.com/tour) on how to use the site properly. – LyzandeR Nov 24 '15 at 16:24

2 Answers2

0

Taking a guess, you probably want

box.labels<-c("Blues","Contra","Swing")
boxplot(new~type, data=droplevels(subset(dance,type %in% box.labels)),
     ylab="Dance Count",
     xlab="Type", name=box.labels,
     main="Dancing for a Healthier You")

The main points here are:

  • Both drop.levels and droplevels are intended for dropping unused levels from a data set. The subset() command above is more likely to do what you want.
  • I'm using droplevels() from base R rather than gdata::drop.levels. droplevels() is a few years old in base R; gdata::drop.levels dates from before it was available).
  • I'm using new~type,data=... instead of dance$new~dance$type; using the data argument where possible is generally more readable and more robust.

It's possible that you don't even need droplevels(); boxplot() might ignore unused levels by default. The name argument might be redundant too.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
0

Thanks for the help. Your tips helped me figure it out.

I ended up plotting subset data which let me avoid the drop levels function. Here is what I used:

dancenew<-subset(Dance, Type=="Lindy" | Type== "Blues" | Type=="Contra")

box.labels<-c("Lindy","Blues","Contra") 
boxplot(dancenew$Count~dancenew$Type, ylab="Dance Count", data=ausportnew, xlab="Type", name=box.labels, main="Dancing for a healthier you")
Vixxen81
  • 33
  • 1
  • 7