Arrange facet_grid with a column without a facet (and labels using a column without a facet)

I have a few questions regarding faceting in ggplot2 ...

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

(note that it is ranked by asc, Alarm asc and two Alarms are ranked 3 because their Totals = 1798 for week 4, and Rank is set 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 it ....

alt text http://img101.imageshack.us/img101/9103/ss20100315112108.png

Note that this is a facet in accordance with the alarm and that the faces are in alphabetical order.

Two questions:

  • How can I save it as an alarm, but displayed in the correct order? (Asc rank, asc alarm).

alt text http://img17.imageshack.us/img17/6986/ss20100315113243.png

  1. Also, how can I save it using Alarm, but show tags from Rank instead of Alarm?

alt text http://img85.imageshack.us/img85/470/ss20100315113529.png

Please note that I can’t just put a face on the Rank, because ggplot2 will see only 3 faces for building, where there are really 4 different alarms.

+4
source share
1 answer

To answer your first question: you can simply reorder the factor levels so that they are no longer in alphabetical order:

 spark$Alarm<-factor(spark$Alarm, levels(spark$Alarm)[c(1,4,2,3)]) 

In the second question, you can write your own labeller function to link Alarms and Ranks, something like

 lbl.fn <- function(variable, value) { paste(spark$Rank[which(as.character(spark$Alarm)==as.character(value))],as.character(value)) } s + facet_grid(Alarm ~ ., labeller="lbl.fn") + geom_line() 
+6
source

Source: https://habr.com/ru/post/1304114/


All Articles