[R] ggplot2: removing empty factor levels in facet_grid

Ista Zahn istazahn at gmail.com
Mon Feb 3 17:38:48 CET 2014


Hi Anna,

It's unclear what you want here, as it doesn't make sense in this
example to remove the empty panels. Doing so will mess up the
alignment of the remaining panels. Perhaps you want

ggplot(mydata,aes(week,values))+
  facet_wrap(~year+station, scales = "fixed", drop=TRUE)+
  scale_shape_manual(values = 1:3) +
  geom_point(mapping=aes(shape=factor(organism)))+
  theme_bw() +
  theme(strip.background = element_blank()) +
  ggtitle("this plot is ugly but ignore that")+
  xlab("my x-axis stuff")+
  ylab("my y-axis stuff")+
  theme(legend.title=element_blank(), legend.key =
          element_blank())

?

Note also that there is a separate ggplot2 mailing list at
https://groups.google.com/forum/#!forum/ggplot2 -- you may wish to
post ggplot2-related questions there in the future.

Best,
Ista

On Mon, Feb 3, 2014 at 6:54 AM, Anna Zakrisson Braeunlich
<anna.zakrisson at su.se> wrote:
> Hi,
>
> I would like to thank you for a great package and for all help I have recieved in the past. To my question:
> I have a plot where I have used facet_grid. I have factor: station (B1, BY31, H2) and factor: year (2007, 2008). The plot has two "plot panels" empty as there are no combinations H2:2007 and BY31:2008. For my own data, there is only one panel empty (I am bad at making appropriate dummy data as I am quite new to this) and I am not allowed to publish it like this. The empty  panel needs to be removed.
>
> I have tried loads of things like removing H2:2007 and BY31:2008 by using:
> mydata_truncated <- mydata[
>   mydata$
>     station != "BY31" |
>     mydata$Year !="2008", ]
>
> and have tried facet_grid(year~station, scales = "fixed", drop=TRUE) this doesn't work as all factors are used, it is only a combination of factor levels that need to go.
> I would be extremely grateful for any help.
>
> The script:
> mydata<- data.frame(week = factor(rep(c("19", "21", "23", "25"), each = 10*3)),
>                     station = factor(rep(c("BY31", "B1", "H2"), each = 40)),
>                     year = factor(rep(c("2007", "2008"), each = 60)),
>                     organism = factor(rep(c("zpl", "ses", "cy"), each = 40)),
>                     values = rnorm(120, mean = rep(c(0, 3, 15), each = 10),
>                                  sd = rep(c(1, 3, 6), each = 10)))
>
>
> p <- ggplot(mydata,aes(week,values))+
>   facet_grid(year~station, scales = "fixed", drop=TRUE)+ #drop=TRUE does not work as all factors are used
>   scale_shape_manual(values = 1:3) +
>   geom_point(mapping=aes(shape=factor(organism)))+
>   theme_bw() +
>   theme(strip.background = element_blank()) +
>   ggtitle("this plot is ugly but ignore that")+
>   xlab("my x-axis stuff")+
>   ylab("my y-axis stuff")+
>   theme(legend.title=element_blank(), legend.key =
>           element_blank())
> p
>
> Anna Zakrisson Braeunlich
> PhD student
>
> Department of Ecology, Environment and Plant Sciences
> Stockholm University
> Svante Arrheniusv. 21A
> SE-106 91 Stockholm
> Sweden/Sverige
>
> Lives in Berlin.
> For paper mail:
> Katzbachstr. 21
> D-10965, Berlin - Kreuzberg
> Germany/Deutschland
>
> E-mail: anna.zakrisson at su.se
> Tel work: +49-(0)3091541281
> Mobile: +49-(0)15777374888
> LinkedIn: http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b
>
>><((((º>`•. . • `•. .• `•. . ><((((º>`•. . • `•. .• `•. .><((((º>`•. . • `•. .• `•. .><((((º>
>
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list