drop unused factor levels

869 views
Skip to first unread message

Zodet, Marc W. (AHRQ)

unread,
Dec 3, 2010, 6:22:48 PM12/3/10
to ggp...@googlegroups.com
I'm trying to plot the density distribution of a variable for a subset of my data.
The distribution is fine, but the unused levels of the variable on which I
subset the data appear in the legend.
 
How do I prevent this from happening (i.e., drop unused factor levels).
 
Thanks.
 
Marc

Brandon Hurr

unread,
Dec 3, 2010, 6:33:13 PM12/3/10
to Zodet, Marc W. (AHRQ), ggp...@googlegroups.com
There is a function in R 2.12.0 called droplevels(). Run your data through that first or in the data= call of your qplot/ggplot call. 

B

?droplevels()

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Bruce Raup

unread,
Dec 3, 2010, 9:44:13 PM12/3/10
to Brandon Hurr, Zodet, Marc W. (AHRQ), ggp...@googlegroups.com
Use the drop=TRUE option to subscripting. Here's an example:

> x=sample(letters, 5, replace=T)
> x
[1] "s" "a" "m" "e" "j"
> y=factor(x, levels=letters)
> y
[1] s a m e j
Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
> y[, drop=T]
[1] s a m e j
Levels: a e j m s


--Bruce

Brandon Bertelsen

unread,
Dec 4, 2010, 11:35:13 AM12/4/10
to ggp...@googlegroups.com
I like all of these options but it's often easiest just to factor() the
variable that has extra levels after subsetting. As in this example:
http://stackoverflow.com/questions/3991870/unwanted-items-on-a-plot/3992497#3992497

I don't know what you're plotting but here's an example where y has more
levels than it requires, factor drops those levels

ggplot(df.subset, aes(x, factor(y)) + geom_bar()

Hadley Wickham

unread,
Dec 8, 2010, 8:49:33 AM12/8/10
to Brandon Bertelsen, ggp...@googlegroups.com
Or, if the order of levels isn't important, don't make it a factor in
the first place - use stringsAsFactors = FALSE when loading the dat.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages