研发ggplot2:来自动态变量的迷你图(基于查询结果)
我编写了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这是一个愚蠢的问题:)
这是显而易见的答案。
Okay that was a dumb question :)
Here's the obvious answer.