R ggplot2: Arrange facet_grid by non-facet column (and labels using non-facet column)

Posted by tommy-o-dell on Stack Overflow See other posts from Stack Overflow or by tommy-o-dell
Published on 2010-03-15T03:45:42Z Indexed on 2010/03/15 3:49 UTC
Read the original article Hit count: 667

Filed under:
|
|

I have a couple of questions regarding facetting in ggplot2...

Let's say I have a query that returns data that looks like this:

(note that it's ordered by Rank asc, Alarm asc and two Alarms have a Rank of 3 because their Totals = 1798 for Week 4, and Rank is set according to Total for Week 4)

   Rank Week                      Alarm Total
      1    1      BELTWEIGHER HIGH HIGH  1000
      1    2      BELTWEIGHER HIGH HIGH  1050
      1    3      BELTWEIGHER HIGH HIGH   900
      1    4      BELTWEIGHER HIGH HIGH  1800
      2    1              MICROWAVE LHS   200
      2    2              MICROWAVE LHS  1200
      2    3              MICROWAVE LHS   400
      2    4              MICROWAVE LHS  1799
      3    1  HI PRESS FILTER 2 CLOG SW  1250
      3    2  HI PRESS FILTER 2 CLOG SW  1640
      3    3  HI PRESS FILTER 2 CLOG SW  1000
      3    4  HI PRESS FILTER 2 CLOG SW  1798
      3    1 LOW PRESS FILTER 2 CLOG SW   800
      3    2 LOW PRESS FILTER 2 CLOG SW  1200
      3    3 LOW PRESS FILTER 2 CLOG SW   800
      3    4 LOW PRESS FILTER 2 CLOG SW  1798

(duplication code below)

Rank = c(rep(1,4),rep(2,4),rep(3,8))
Week = c(rep(1:4,4))
Total = c(  1000,1050,900,1800,
        200,1200,400,1799,
        1250,1640,1000,1798,
        800,1200,800,1798) 
Alarm = c(rep("BELTWEIGHER HIGH HIGH",4),  
        rep("MICROWAVE LHS",4), 
        rep("HI PRESS FILTER 2 CLOG SW",4), 
        rep("LOW PRESS FILTER 2 CLOG SW",4)) 
spark <- data.frame(Rank, Week, Alarm, Total) 

Now when I do this...

s <- ggplot(spark, aes(Week, Total)) +          
     opts( 
        panel.background = theme_rect(size = 1, colour = "lightgray"), 
        panel.grid.major = theme_blank(), 
        panel.grid.minor = theme_blank(), 
        axis.line = theme_blank(), 
        axis.text.x = theme_blank(), 
        axis.text.y = theme_blank(), 
        axis.title.x = theme_blank(), 
        axis.title.y = theme_blank(),  
        axis.ticks = theme_blank(), 
        strip.background = theme_blank(), 
        strip.text.y = theme_text(size = 7, colour = "red", angle = 0) 
    ) 

s + facet_grid(Alarm ~ .) + geom_line() 

I get this....

alt text

Notice that it's facetted according to Alarm and that the facets are arranged alphabetically.

Two Questions:

  1. How can I can I keep it facetted by alarm but displayed in the correct order? (Rank asc, Alarm asc).

alt text

  1. Also, how can I keep it facetted by alarm but show labels from Rank instead of Alarm?

alt text

Note that I can't just facet on Rank because ggplot2 would see only 3 facets to plot where there are really 4 different alarms.

Thanks kindly for the help!

Tommy

© Stack Overflow or respective owner

Related posts about r

    Related posts about ggplot2