Home > front end >  Plotting time series data
Plotting time series data

Time:04-20

Does anyone know how i can plot this time series data? I'm trying to plot this data with each substation having a value over a 24h period, however im struggling to plot this as i dont know has to pass the times in a plot.

If poss something line this would be good with a line representing each substation (trend line would also be good but can add that in later);

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

data snipet;

 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.

CodePudding user response:

You could get a line for each Substation over time by converting your data to long or tidy format using e.g. 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()

DATA

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