R& ggplot2:来自动态变量的迷你图(基于查询结果)

时间:2010-02-25 01:59:05

标签: r visualization ggplot2

我写了一个SQL查询,告诉我前一周最常见的10个警报的名称。我写了一个查询,收集了前10个警报,并为每个警报提供每周年度总计。

现在我正在寻找创建一系列迷你图,显示本周十大警报中的每一个的年初至今趋势。

我得到了类似于我喜欢的东西,但我现在需要让它“充满活力”。即使它没有硬编码警报的名称使其工作(因为这些将随着每周的SQL查询而改变)。

如何在不对报警名称进行硬编码的情况下更改下面的R代码?

级别(spark $ Alarm)是否与某些事情有关?

非常感谢您的建议: - )

Week = c(rep(1:8,2))
Total = rnorm(16,1000,600)
Alarm = c(rep("BELTWEIGHER HIGH HIGH",8), rep("MICROWAVE LHS",8))
spark <- data.frame(Week, Alarm, Total)

s <- ggplot(spark, aes(Week, Total)) +
     facet_grid(Alarm ~ ., scales = "free", as.table = FALSE) +
     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 = 90)
 )

s1 <- s  + geom_line(subset = .(Alarm == "BELTWEIGHER HIGH HIGH"))
s2 <- s1 + geom_line(subset = .(Alarm == "MICROWAVE LHS"))
s2

1 个答案:

答案 0 :(得分:4)

好的,这是一个愚蠢的问题:)

这是明显的答案。

Week = c(rep(1:8,2))
Total = rnorm(16,1000,600)
Alarm = c(rep("BELTWEIGHER HIGH HIGH",8), rep("MICROWAVE LHS",8))
spark <- data.frame(Week, Alarm, Total)


s <- ggplot(spark, aes(Week, Total)) +         
     theme(
        panel.background = element_rect(size = 1, colour = "lightgray"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(), 
        axis.ticks = element_blank(),
        strip.background = element_blank(),
        strip.text.y = element_blank()
        #strip.text.y = element_text(size = 7, colour = "red", angle = 90)
    )

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