Skip to content
Prev 170248 / 398506 Next

barplot() x axes are not updated after removal of categories from the dataframe

on 02/12/2009 06:35 AM R User R User wrote:
When you subset a factor, the unused levels are not removed by default.
This is intentional to preserve these characteristics for subsequent
operations such as modeling.

You are presumably using something like:

  table(final$varname)

to create the counts used in barplot().

As an example, using the iris dataset:
setosa versicolor  virginica
        50         50         50


iris2 <- subset(iris, Species != "setosa")
setosa versicolor  virginica
         0         50         50

Note that even though I removed records from iris where Species ==
"setosa", it still shows in the table.

You would need to do the following:

iris2$Species <- factor(iris2$Species)
versicolor  virginica
        50         50

Using factor() on the column removes the unused levels.

See  ?"[.factor" for more information.

HTH,

Marc Schwartz