Remove Unused Categorical Values Boxplot - R

Remove Unused categorical values boxplot - R

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)

Sample Image

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

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

How to remove ticks and labels of dropped off factors in a box plot

Subset the data.frame to not include those levels. Don't forget to drop levels.

myx <- droplevels(diamonds[diamonds$cut %in% c("Fair","Good","Very Good"), ])
boxplot(price~cut, data=myx)

Drop factor combinations in r boxplot

The following gives a plot, where the gaps are omitted:

boxplot(crecimiento ~ interaction(localidad, transgen, drop = TRUE),
data = na.omit(anova_propagacion), las = 2)

Sample Image

The two main changes compared to your code are:

  • To calculate the interaction term in the formula I use interaction(localidad, transgen, drop = TRUE). Setting drop = TRUE means that combinations that do not actually appear in the data are omitted.

  • This alone is not enough, because certain combinations do appear in the data, but the value of crecimento is NaN (e.g., SINALOA.N). Therefore, they are not dropped, but there is still nothing to be plotted. This is solved by just removing those rows with na.omit().

Drop unused factor levels in a subsetted data frame

All you should have to do is to apply factor() to your variable again after subsetting:

> subdf$letters
[1] a b c
Levels: a b c d e
subdf$letters <- factor(subdf$letters)
> subdf$letters
[1] a b c
Levels: a b c

EDIT

From the factor page example:

factor(ff)      # drops the levels that do not occur

For dropping levels from all factor columns in a dataframe, you can use:

subdf <- subset(df, numbers <= 3)
subdf[] <- lapply(subdf, function(x) if(is.factor(x)) factor(x) else x)

Remove unused factor levels from a ggplot bar plot

One easy options is to use na.omit() on your data frame df to remove those rows with NA

ggplot(na.omit(df), aes(x=name,y=var1)) + geom_bar()

Given your update, the following

ggplot(df[!is.na(df$var1), ], aes(x=name,y=var1)) + geom_bar()

works OK and only considers NA in Var1. Given that you are only plotting name and Var, apply na.omit() to a data frame containing only those variables

ggplot(na.omit(df[, c("name", "var1")]), aes(x=name,y=var1)) + geom_bar()

How to remove NA from a factor variable (and from a ggplot chart)?

assuming your data is in a data frame called dat

newdat <- dat[!is.na(dat$Factor), ]

not sure how to solve the problem inside of ggplot code

How can I remove empty factors from ggplot2 facets?

EDIT Updated to ggplot2 0.9.3

Here's another solution. It uses facet_grid and space = "free"; also it uses geom_point() and geom_errorbarh(), and thus there is no need for coord.flip(). Also, the x-axis tick mark labels appear on the lower panel only. In the code below, the theme command is not essential - it is used to rotate the strip text to appear horizontally. Using the test dataframe from above, the following code should produce what you want:

library(ggplot2)

p <- ggplot(test, aes(y = characteristic, x = es, xmin = ci_low, xmax = ci_upp)) +
geom_point() +
geom_errorbarh(height = 0) +
facet_grid(set ~ ., scales = "free", space = "free") +
theme_bw() +
theme(strip.text.y = element_text(angle = 0))

p

The solution is based on the example on page 124 in Wickham's ggplot2 book.



Related Topics



Leave a reply



Submit