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

发布于 2024-08-22 20:52:41 字数 1175 浏览 6 评论 0原文

我编写了一个 SQL 查询,它告诉我前一周最常见的 10 个警报的名称。我编写了一个查询,获取前 10 个警报,并提供每个警报的年初至今每周总计。

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

我得到了一些类似于我想要的东西,但我现在需要让它变得“动态”。也就是说,无需对警报名称进行硬编码即可使其正常工作(因为这些名称每周都会随 SQL 查询而变化)。

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

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

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.

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫轩蝶泪 2024-08-29 20:52:41

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

这是显而易见的答案。

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

Okay that was a dumb question :)

Here's the obvious answer.

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文