boxplot() 仍然显示我通过删除相应行而删除的类别的框

boxplot() still displays boxes for categories I've dropped by deleting corresponding rows

我通过从 2 个类别中删除数据,从现有数据框 antiques 派生了一个新数据框 major.groups

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

但是,当我使用boxplot()时,BoxesMetalware仍然出现在x轴上(显然没有对应的数据)。

在使用新数据框 major.groups 时如何排除这些?我显然可以在 R 之外删除它们并重新导入 - 但我相信一定有更好的方法。

非常感谢。

没有可重现的示例,我无法对此进行测试。但这应该有效:

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

请注意 boxplot() 显示所有现有因子水平的值:

 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’).

对于一个因素来说,降低数值并不意味着降低水平。试试这个:

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