研发ggplot2:动态变量中的迷你图(基于查询结果) [英] R & ggplot2: Sparklines from dynamic variables (based on query results)

查看:55
本文介绍了研发ggplot2:动态变量中的迷你图(基于查询结果)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个SQL查询,告诉我前一周最常见的10个警报的名称.而且我编写了一个查询,该查询接收了前10个警报,并为每个警报提供了年初至今的总计.

I've written an SQL query that tells me the names of the previous week's top 10 most frequent Alarms. And I've written a query that takes those top 10 alarms and provides a YTD weekly totals for each of those alarms.

现在,我希望创建一个迷你图面板,以显示本周前10条警报中的每个年初至今的趋势.

Now I'm looking to create a panel of sparklines showing the YTD trend for each of the week's top 10 alarms.

我得到了与我想要的东西相似的东西,但是现在我需要使其变得动态".也就是说,在不对警报名称进行硬编码的情况下使其正常工作(因为这些名称会随SQL查询每周更改一次).

I got something resembling what I'd like, but I now need to make it "dynamic". i.e. to make it work without hardcoding the names of the alarms (since these will change with the SQL query every week).

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

How can I go about changing the R code below to work without hardcoding the names of the alarms?

关卡(spark $ Alarm)有关系吗?

Does levels(spark$Alarm) have something to do with?

非常感谢您的建议:-)

Thanks kindly for the advice :-)

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

推荐答案

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

Okay that was a dumb question :)

这是显而易见的答案.

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()

这篇关于研发ggplot2:动态变量中的迷你图(基于查询结果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆