按非facet列排列facet_grid(以及使用非facet列的标签)
我有几个关于 ggplot2 中分面的问题...
假设我有一个查询返回如下所示的数据:(请
注意,它是按 Rank asc、Alarm asc 排序的,并且两个 Alarms 的 Rank 为 3,因为它们的总计 =第 4 周为 1798,排名根据第 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
(下面是重复代码)
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)
现在,当我这样做时...
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()
我得到了...
alt text http://img101.imageshack.us/img101/9103/ss20100315112108.png
请注意,它是根据 Alarm 进行分面的,并且分面按字母顺序排列。
两个问题:
- 我怎样才能让它被警报分面但以正确的顺序显示? (排名升序,警报升序)。
替代文本 http://img17.imageshack.us/img17/6986/ss20100315113243.png< /a>
- 另外,我怎样才能让它保持闹钟的多面性,但显示等级而不是闹钟的标签?
替代文本 http://img85.imageshack.us/img85/470/ss20100315113529.png< /a>
请注意,我不能只在 Rank 上进行分面,因为 ggplot2 只会看到 3 个分面来绘制实际上有 4 个不同警报的位置。
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 http://img101.imageshack.us/img101/9103/ss20100315112108.png
Notice that it's facetted according to Alarm and that the facets are arranged alphabetically.
Two Questions:
- How can I can I keep it facetted by alarm but displayed in the correct order? (Rank asc, Alarm asc).
alt text http://img17.imageshack.us/img17/6986/ss20100315113243.png
- Also, how can I keep it facetted by Alarm but show labels from Rank instead of Alarm?
alt text http://img85.imageshack.us/img85/470/ss20100315113529.png
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要回答您的第一个问题:您可以简单地重新排序因子级别,以便它们不再按字母顺序排列,如下所示:
对于第二个问题,您可以编写自己的贴标器函数,以便将警报和排名关联起来,例如
To answer your first question: you could simply reorder factor levels so that they are no longer alphabetical, like so:
For the second question, you could write your own labeller function so associate Alarms and Ranks, something like