绘制时间序列数据

Plotting time series data

有谁知道我如何绘制这个时间序列数据?我试图用每个变电站在 24 小时内都有一个值来绘制这些数据,但是我很难绘制它,因为我不知道必须在图中传递时间。

如果有一条线,最好用一条线代表每个变电站(趋势线也不错,但可以稍后添加);

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.

随着时间的推移,您可以为每个 Substation 获取一行,方法是使用例如将数据转换为长或整齐的格式。 tidyr::pivot_longer:

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"
))