我需要在我的資料集中為每個時間序列(列)制作多個單獨的圖:
https://github.com/rhozon/datasets/raw/master/multiple_time_series_dataset.csv
我想到了一些回圈遍歷每一列并單獨繪制每個圖形的回圈。
ggplot(df, aes(x = timestamp,
y = for loop for each column) )
geom_line()
如何通過為資料集的每一列生成時間圖來節省時間?
uj5u.com熱心網友回復:
您可以嘗試使用 lapply 而不是 for 回圈來執行以下代碼。
# transforming timestamp in date object
df$timestamp <- as.Date(df$timestamp, format = "%d/%m/%Y")
# create function that is used in lapply
plotlines <- function(variables){
ggplot(df, aes(x = timestamp, y = variables))
geom_line()
}
# plot all plots with lapply
plots <- lapply(df[names(df) != "timestamp"], plotlines) # all colums except timestamp
plots
uj5u.com熱心網友回復:
也許這就是你要找的
library(tidyverse)
library(lubridate)
library(plotly)
df <- vroom::vroom("https://github.com/rhozon/datasets/raw/master/multiple_time_series_dataset.csv")
df <- df %>%
mutate(timestamp = dmy(timestamp))
VARS <- names(df)[-1][1:3]
map(.x = VARS,
.f = ~ ggplot(df, aes(x = timestamp, y = .data[[.x]]))
geom_line()) %>%
map(ggplotly)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381242.html
上一篇:ggplot2垂直顏色條標題居中
