3

I have the following code:

x = rnorm(30, 1, 1)
c = c(rep("x1",10), rep("x2",10), rep("x3",10))
df = dataframe(x,c)
boxplot(x ~ c, data=df)

It works great. But if I decide I am no longer interested in seeing x3, remove it, and replot:

dfMod = subset(df, c %in% c("x1", "x2"))
boxplot(x ~ c,data=dfMod)

The boxplot still shows a column for x3.

enter image description here

Ive tried giving boxplot a hint using

boxplot(x~c,data=dfMod, names = c("x1", "x2"))

but this throws the error that names size is not right. Thanks in advance for the help

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
JHowIX
  • 1,683
  • 1
  • 20
  • 38

1 Answers1

9

Use droplevels after subset

dfMod <- subset(df, c %in% c("x1", "x2"))    
dfMod$c <- droplevels(dfMod$c)
boxplot(x ~ c,data=dfMod)

You can also use class to change factor to character and subsetting inside boxplot call

class(df) <- c("numeric", "character")
boxplot(x ~ c, subset=c %in% c("x1", "x2"),  data=df)

enter image description here

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
  • So obviously this is an easy thing to do and I feel a little embarrassed for having to ask it on SO. So I can get better handle on R terminology, what should I have googled to find the droplevels command? "Remove unused categorical value from data frame column" didn't prove particularly helpful – JHowIX Nov 13 '13 at 22:28
  • If you write "get rid of unused factors in r" on google will give you lots of results about dropping unused levels ;) – Jilber Urbina Nov 14 '13 at 09:24