我有一個串列中的資料框,對應于來自同一個調查的幾個變數。例如,對應于第一個變數的資料框如下所示:
Value = c(10, 12, 14, 11)
Quarter = c(1, 2, 3, 4)
dt = as.data.frame(cbind(Quarter, Value))
dt
Value Quarter
10 1
12 2
14 3
11 4
后續遵循相同的模式。串列中單個元素的繪圖是使用 ggplot2 創建的:
ggplot(data = dt, aes(x=Quarter, y=Value))
geom_line()
現在,我需要為串列中的每個元素(變數)創建一個類似上面的圖,并將它們保存在我的磁盤上。是否可以使用 R 來做到這一點?
問候,
uj5u.com熱心網友回復:
是的。最直接的方式是for回圈:
for(i in seq_along(your_list)) {
p = ggplot(data = your_list[[i]], aes(x=Quarter, y=Value))
geom_line()
labs(title = paste("Plot", names(your_list)[i])
ggsave(
paste0("plot_", i, ".png"),
plot = p
)
}
當然,您可以根據需要進行自定義。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358874.html
