【发布时间】:2020-07-22 07:10:01
【问题描述】:
我想在一张图表中比较真实世界的数据和模拟数据。代码应该接受任意数量的线来绘制。我想出了这个:
simulationRuns <- 5 #Variable to be changed depending on how many simulations were made
plotLoop <- ggplot() +
geom_line(data = relWorldData,
mapping = aes(x = DateTime, y = VALUE, color = "realWorldData"))
for (i in 1:simulationRuns){
plotLoop <- plotLoop +
geom_line(data = listOfSimResults[[i]],
mapping = aes(x = DateTime, y = VALUE, color = paste0("simRun-", i)))
}
figureLoop <- ggplotly(plotLoop)
问题是,所有行都显示为 simRun-5,因此不是独立的 -
我是 R 新手,所以请多多包涵;) 在此先感谢帕特里克
后续问题 bc。在评论中阅读代码很糟糕:
我阅读了 Lapply 并将代码重写为:
plotLoop <- ggplot() + geom_line(data = relWorldData, mapping = aes(x = DateTime, y = VALUE, color = "RealWorldData"))
addGeomLine <- function (i, obj){
obj <- obj +
geom_line(data = listOfSimResults[[i]], mapping = aes(x = DateTime, y = VALUE, color = paste0("simRun-", i)))
}
lapply(1:runs, addGeomLine, plotLoop)
figureLoop <- ggplotly(plotLoop)
这一次,只显示 RealWorldData,但没有显示任何 Simulations。你能告诉我我错过了什么吗?
【问题讨论】: