Skip to content

Instantly share code, notes, and snippets.

@andremueller
Last active July 22, 2019 19:55
Show Gist options
  • Save andremueller/6155b227e604c28e0b3a21e769df3086 to your computer and use it in GitHub Desktop.
Save andremueller/6155b227e604c28e0b3a21e769df3086 to your computer and use it in GitHub Desktop.
Plotting time-series with ggplot
library(ggplot2)
library(tidyverse)
requireNamespace("xts")
requireNamespace("generics")
requireNamespace("lubridate")
n <- 100
t1 <- lubridate::ymd(20190101)
tser <- xts::xts(sin(2 * pi * seq(0, 1, length.out = n)),
order.by = t1 + lubridate::minutes(0:(n - 1)))
# plotting that time series (using xts)
plot(tser)
# reformat time series as tibble
tser2 <- tser %>% generics::tidy()
# plotting that time series with ggplot
p <- tser2 %>%
ggplot(aes(x = index, y = value)) +
scale_x_datetime(date_labels = '%Y-%m-%d %H:%M:%S') +
geom_line()
print(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment