Last active
July 22, 2019 19:55
-
-
Save andremueller/6155b227e604c28e0b3a21e769df3086 to your computer and use it in GitHub Desktop.
Plotting time-series with ggplot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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