簡體   English   中英

繪制時間序列數據

[英]Plotting time series data

有誰知道我怎么能 plot 這個時間序列數據? 我正在嘗試 plot 這個數據,每個變電站在 24 小時內都有一個值,但是我很難達到 plot,因為我不知道必須在 plot 中傳遞時間。

如果有一條線,最好用一條線代表每個變電站(趨勢線也不錯,但可以稍后添加);

ggplot(substation_average, aes(x=Date, y=Substation)) +
    geom_line()

數據片段;

 Substation   `00:00` `00:10` `00:20` `00:30` `00:40` `00:50` `01:00` `01:10`
       <int>    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
1     511016    288.    289.    283.    277.    273.    268.    262.    256.
2     511029    106.    120.    127.    132.    143.    140.    137.    134.
3     511030    160.    156.    150.    144.    140.    138.    136.    133.
4     511033    181.    175.    181.    180.    177.    177.    179.    176.
5     511034    305.    302.    296.    295.    290.    286.    281.    280.
6     511035    133.    138.    134.    128.    127.    124.    120.    118.

隨着時間的推移,您可以通過使用tidyr::pivot_longer將數據轉換為長格式或整潔格式來為每個Substation獲取一條線:

library(tidyr)
library(ggplot2)

substation_average <- substation_average |>
  pivot_longer(-Substation, names_to = "time")

head(substation_average)
#> # A tibble: 6 × 3
#>   Substation time  value
#>        <int> <chr> <dbl>
#> 1     511016 00:00   288
#> 2     511016 00:10   289
#> 3     511016 00:20   283
#> 4     511016 00:30   277
#> 5     511016 00:40   273
#> 6     511016 00:50   268

ggplot(substation_average, aes(x = time, y = value, color = factor(Substation), group = Substation)) +
  geom_line()

數據

substation_average <- structure(list(
  Substation = c(
    511016L, 511029L, 511030L, 511033L,
    511034L, 511035L
  ), `00:00` = c(288, 106, 160, 181, 305, 133),
  `00:10` = c(289, 120, 156, 175, 302, 138), `00:20` = c(
    283,
    127, 150, 181, 296, 134
  ), `00:30` = c(
    277, 132, 144, 180,
    295, 128
  ), `00:40` = c(273, 143, 140, 177, 290, 127), `00:50` = c(
    268,
    140, 138, 177, 286, 124
  ), `01:00` = c(
    262, 137, 136, 179,
    281, 120
  ), `01:10` = c(256, 134, 133, 176, 280, 118)
), class = "data.frame", row.names = c(
  "1",
  "2", "3", "4", "5", "6"
))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM