gpt4 book ai didi

R&ggplot2 : Sparklines from dynamic variables (based on query results)

转载 作者:行者123 更新时间:2023-12-01 13:07:04 25 4
gpt4 key购买 nike

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

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

我得到了一些与我想要的类似的东西,但我现在需要使它“动态”。即使其在不对警报名称进行硬编码的情况下工作(因为这些名称每周都会随着 SQL 查询而改变)。

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

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

最佳答案

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

这是显而易见的答案。

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

关于R&ggplot2 : Sparklines from dynamic variables (based on query results),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2331070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com