我已經運行了我首先過濾變數然后運行繪圖的代碼,請參見下面的代碼
pl1 = mydata %>%
filter(CID =="1", NID =="1", SID =="301", WID =="1")
ggplot(pl1, aes(x=Timestamp, y=T_Var , colour=SID))
geom_line()
geom_hline(yintercept=.4, linetype="dashed", color = "red")
geom_hline(yintercept=.7, linetype="dashed", color = "maroon")
是否每個過濾專案如下 CID 從 1 - 30 NID 從 1 - 8 SID 從 301 - 310 WID 從 1 - 9 T_Var 是目標變數
我知道這是很多情節,但我想知道這是否可以通過 for 回圈來完成。
uj5u.com熱心網友回復:
我提出這個:
for(cid in 1:30) {
for(nid in 1:8) {
data <- mydata %>% filter(CID==cid, NID==nid, ...)
p <- ggplot(mydata ....)
print(p) # Or save to file using ggsave(), png() or pdf()
}
}
在過濾時,您應該檢查是否有必要將數字轉換為字串。我不知道你的資料框列是什么型別。因此filter(CID==cid),您需要撰寫filter(CID==as.character(cid).
也許有一種更清潔的方法可以做到這一點。堆疊多個 for 回圈可以使組合爆炸......您還可以使用布局面板(https://ggplot2.tidyverse.org/reference/facet_grid.html)制作多個子圖,但要構建多個子圖,這可能是不合適的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/492628.html
上一篇:回圈遍歷集合并組合元素
