Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

Is it possible to drop levels that are not used in ggplot2s facets? This is my code:

tab = as.data.frame(cbind(groups = mtcars$cyl, names = row.names(mtcars), val = mtcars$mpg, N = mtcars$disp))
tab$N = as.numeric(tab$N)

ggplot(tab, aes(names,val)) + 
geom_point() + coord_flip() + 
theme_bw() + 
facet_grid(groups ~ ., drop=TRUE)#, scales="free", as.table=F, space="free")

I tried the drop=T switch but it doesnt help. What am I doing wrong?

Thanks for any help.

share|improve this question

1 Answer

up vote 1 down vote accepted

Your example data just doesn't have any unused levels to drop. Check the behavior in this example:

dat <- data.frame(x = runif(12),
                  y = runif(12),
                  grp1 = factor(rep(letters[1:4],times = 3)),
                  grp2 = factor(rep(LETTERS[1:2],times = 6)))

levels(dat$grp2) <- LETTERS[1:3]

ggplot(dat,aes(x = x,y = y)) + 
    facet_grid(grp1~grp2,drop = FALSE) + 
    geom_point()

ggplot(dat,aes(x = x,y = y)) + 
    facet_grid(grp1~grp2,drop = TRUE) + 
    geom_point()

It may be that you're looking to change which factors are plotting on the vertical axis in each facet, in which case you want to set the scales argument and use facet_wrap:

ggplot(tab, aes(names,val)) + 
    geom_point() + coord_flip() + 
    theme_bw() + 
    facet_wrap(~groups,nrow = 3,scales = "free_x")
share|improve this answer
Oh i see now what it does. My intention was to plot only those levels in every facet, that are actually grouped by the facet. Like, using my tab example,dotchart(as.numeric(tab$val), labels=tab$names, groups=tab$groups). Is it possible? – mrrrau Mar 17 '12 at 16:30
@mrrrau Yes, see my edit. – joran Mar 17 '12 at 16:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.