Remove unused categorical boxplot values ​​- R

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 that I am no longer interested in seeing x3, remove it and replace it:

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

      

The boxplot field still displays the column for x3.

enter image description here

I've tried to give a boxplot hint with

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

      

but this throws an error that did not specify the size of the name. Thanks in advance for your help

0


source to share


1 answer


Use droplevels

aftersubset

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 subset within a boxplot

call



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

      

enter image description here

+3


source







All Articles