[R] ggpliot2: reordering of factors in facets facet.grid(). Reordering of factor on x-axis no problem.

Ista Zahn istazahn at gmail.com
Thu Mar 7 18:14:01 CET 2013


Hi Anna,

On Thu, Mar 7, 2013 at 10:16 AM, Anna Zakrisson <anna at ecology.su.se> wrote:
>
> Hi everyone (again),
> before you all start screaming that the reordering of factors has been
> discusse on several threads and is not particular to ggplot2, hear me out.

I'm sorry you have been traumatized like this! I promise not to yell.

> I can easily reorder my x-axis factor in facet.grid() in ggplot2. What I
> cannot reorder are the factors represented on the strips. I can see that
> the
> graphs are changing, so I am afraid of what it is I am doing. Why is
> ggplot2
> not changin the strip labels if indeed the factor-order has been changed?
> or have I missed to add code defining the factor-labels in the strips. I
> have not found such code as it has been done automatically from my
> dataframe
> using facet.grid() previously. I am simply afraid of displaying the wrong
> things without noticing it.

Your problem is simple. factor1 is a factor with the desired levels.
But you did not facet by factor1, you faceted by Grouping. You just
need to replace

  facet_wrap(~Grouping, ncol = 2)

with

  facet_wrap(~factor1, ncol = 2)

Best,
Ista

> Here is my code and comments:
>
> I want to have my factor 1 in the order: "F", "E", "C", "D", "A", "B"
> instead of alphabetical. My normal methods do not work.
>
> with kind regards
> Anna Zakrisson
>
> # Some dummy data:
> mydata<- data.frame(factor1 = factor(rep(LETTERS[1:6], each = 80)),
>                     factor2 = factor(rep(c(1:5), each = 16)),
>                     factor3 = factor(rep(c(1:4), each = 4)),
>                     var1 = rnorm(120, mean = rep(c(0, 3, 5), each = 40),
>                                  sd = rep(c(1, 2, 3), each = 20)),
>                     var2 = rnorm(120, mean = rep(c(6, 7, 8), each = 40),
>                                  sd = rep(c(1, 2, 3), each = 20)))
>
> # Trying to change the order of my factor1 (same method works for factor3,
> # which is on th x-axis). Factor1 (A-F) is represented on the different
> facets
> # and the factor1 labels can be read off the strips:
> mydata$factor1 <- factor(mydata$factor1,
>                        levels=c("F", "E", "C", "D", "A", "B"),
>                        order=T)
>
> # summarizing the data with factor1 and factor3:
> Summ  <-   ddply(mydata, .(factor3,factor1), summarize,
>                  mean = mean(var1, na.rm = FALSE),
>                  sdv = sd(var1, na.rm = FALSE),
>                  se = 1.96*(sd(var1, na.rm=FALSE)/sqrt(length(var1))))
> Summ$Grouping <- c("F", "E", "C", "D", "A", "B")[Summ$factor1]
>
> # Trying to order factor 1 accordingly:
> Summ$factor1 <- factor(Summ$factor1,
>                        levels=c("F", "E", "C", "D", "A", "B"),
>                        order=T)
>
>
> # plotting:
> ggplot(Summ, aes(factor3, mean, group = factor1,
>                  ymin = mean - sdv , ymax = mean + sdv)) +
>   geom_point(position = position_dodge(width = 0.25), size = 3) +
>   geom_line(position = position_dodge(width = 0.25)) +
>   geom_errorbar(width = 0.3, position = position_dodge(width = 0.25), size
> =
> 0.3) +
>   facet_wrap(~Grouping, ncol = 2) +
>   theme(strip.background = element_blank()) +
>   scale_shape(solid = FALSE)+
>   theme_bw() +
>   ylab(expression(paste("my measured stuff"))) +
>   xlab("factor3") +
>   theme(legend.position="none")+
>   labs(shape = "factor1", group = "factor1", linetype = "factor1")
>
> # What I find really scary is that when excluding the parts:
> mydata$factor1 <- factor(mydata$factor1,
>                          levels=c("F", "E", "C", "D", "A", "B"),
>                          order=T)
> Summ$factor1 <- factor(Summ$factor1,
>                        levels=c("F", "E", "C", "D", "A", "B"),
>                        order=T)
> # I actually get a different plot than had I not reordered the factors.
> # The labels are however still the same. How do I solve this?
>
>
>
> ### But if I change the factor order for factor 3 (on the x-axis) it
> works!
> I get
> # a different factor order!
> Summ$factor1 <- factor(Summ$factor1,
>                        levels=c("4", "2", "3", "1"),
>                        order=T)
>
>
> ggplot(Summ, aes(factor3, mean, group = factor1,
>                  ymin = mean - sdv , ymax = mean + sdv)) +
>   geom_point(position = position_dodge(width = 0.25), size = 3) +
>   geom_line(position = position_dodge(width = 0.25)) +
>   geom_errorbar(width = 0.3, position = position_dodge(width = 0.25), size
> =
> 0.3) +
>   facet_wrap(~Grouping, ncol = 2) +
>   theme(strip.background = element_blank()) +
>   scale_shape(solid = FALSE)+
>   theme_bw() +
>   ylab(expression(paste("my measured stuff"))) +
>   xlab("factor3") +
>   theme(legend.position="none")+
>   labs(shape = "factor1", group = "factor1", linetype = "factor1")
>
>
> Anna Zakrisson Braeunlich
> PhD student
>
> Department of Ecology Environment and Plant Sciences
> Stockholm University
> Svante Arrheniusv. 21A
> SE-106 91 Stockholm
> Sweden
>
> 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