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

John Kane jrkrideau at inbox.com
Thu Mar 7 19:44:47 CET 2013


   Anna,
   You're right.  Something I am doing seems to be messing up RStudio every
   once in a while.  I rebooted it and the faceting is working just fine both
   with just the required packages loaded and with my normal set of packages.
   I had real trouble getting the legend.position command to work until I
   rebooted RStudio and now this.
   I have no idea of how to track  down what's happening but at least you've
   made me aware of it.
   Thanks

   John Kane
   Kingston ON Canada

   -----Original Message-----
   From: anna at ecology.su.se
   Sent: Thu, 07 Mar 2013 19:32:21 +0100
   To: jrkrideau at inbox.com, istazahn at gmail.com
   Subject: Re: [R] ggpliot2: reordering of factors in facets facet.grid().
   Reordering of factor on x-axis no problem.

   Dear John and Ista,

   Ista: Thank you so much for your help and for not shouting :-)
   Sometimes one goes blind having stared at a script for too long. I realize
   that the grouping is not needed at all - it is the remnant from another
   figure I made showing two factor 1 -levels per plot instead of just one
   level.

   John:   It   works   for   me.   You  mentioned  previously  that  the
   geometric.line(blabla dodge) didn't work because you had some other packages
   open. Maybe that is the case again? If you find out what it is, please
   share! It is good to know which packages disturb each other!

   Once again, thank you so much! Another day closer to disputation... :-S
   Anna


   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
   ><((((º>`•. . • `•. .• `•. . ><((((º>`•. . • `•. .• `•. .><((((º>`•. . •
   `•. .• `•. .><((((º>

     -----Original Message-----
     From: John Kane <jrkrideau at inbox.com>
     To: Ista Zahn <istazahn at gmail.com>, Anna Zakrisson <anna at ecology.su.se>
     Cc: "r-help at r-project.org" <r-help at r-project.org>
     Date: Thu, 7 Mar 2013 09:35:01 -0800
     Subject: Re: [R] ggpliot2: reordering of factors in facets facet.grid().
     Reordering of factor on x-axis no problem.

   If I try  facet_wrap(~factor1, ncol = 2) I get no faceting at all. Strange.

   John Kane
   Kingston ON Canada
   > -----Original Message-----
   > From: istazahn at gmail.com
   > Sent: Thu, 7 Mar 2013 12:14:01 -0500
   > To: anna at ecology.su.se
   > Subject: Re: [R] ggpliot2: reordering of factors in facets facet.grid().
   > Reordering of factor on x-axis no problem.
   >
   > 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:
   >> [1]http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b
   >>
   >> ><((((º>`•. . • `•. .• `•. . ><((((º>`•. . • `•. .•
   >> `•. .><((((º>`•. . • `•. .• `•. .><((((º>
   >>
   >>         [[alternative HTML version deleted]]
   >>
   >>
   >> ______________________________________________
   >> R-help at r-project.org mailing list
   >> [2]https://stat.ethz.ch/mailman/listinfo/r-help
   >> PLEASE do read the posting guide
   >> [3]http://www.R-project.org/posting-guide.html
   >> and provide commented, minimal, self-contained, reproducible code.
   >>
   >
   > ______________________________________________
   > R-help at r-project.org mailing list
   > [4]https://stat.ethz.ch/mailman/listinfo/r-help
   > PLEASE do read the posting guide
   > [5]http://www.R-project.org/posting-guide.html
   > and provide commented, minimal, self-contained, reproducible code.
   ____________________________________________________________
   FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your
   desktop!
   Check it out at [6]http://www.inbox.com/marineaquarium

     _________________________________________________________________

   [7]3D Earth Screensaver Preview 
   Free 3D Earth Screensaver
   Watch   the   Earth   right   on   your   desktop!  Check  it  out  at
   [8]www.inbox.com/earth

References

   1. http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b
   2. https://stat.ethz.ch/mailman/listinfo/r-help
   3. http://www.R-project.org/posting-guide.html
   4. https://stat.ethz.ch/mailman/listinfo/r-help
   5. http://www.R-project.org/posting-guide.html
   6. http://www.inbox.com/marineaquarium
   7. http://www.inbox.com/earth
   8. http://www.inbox.com/earth


More information about the R-help mailing list