0

I have derived a new data frame major.groups from an existing data frame antiques by removing data from 2 categories:

major.groups <- antiques[antiques$Group!="Boxes" & antiques$Group!="Metalware",]

However, when I use boxplot(), Boxes and Metalware still appear on the x-axis (obviously with no corresponding data).

How can I exclude these when working with the new data frame major.groups? I can obviously remove them outside of R and re-import - but I'm sure there must be a better way.

Many thanks in advance.

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
Bullitt
  • 13
  • 1
  • 4

1 Answers1

0

Without reproducible example, I am unable to test this. But this should work:

major.groups <- droplevels(antiques[antiques$Group!="Boxes" & antiques$Group!="Metalware",])

Note that boxplot() displays values for all existing factor levels:

 If multiple groups are supplied either as multiple arguments or
 via a formula, parallel boxplots will be plotted, in the order of
 the arguments or the order of the levels of the factor (see
 ‘factor’).

For a factor, dropping values does not mean dropping levels. Try this:

x <- factor(letters[1:4])
#[1] a b c d
#Levels: a b c d
x[-1]
#[1] b c d
#Levels: a b c d
droplevels(x[-1])
#[1] b c d
#Levels: b c d
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248