Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to ggplot an xts time series in r?

The call to plot() below works, while the call to ggplot() does not. I have very poor vision, perhaps I am missing something obvious?

library(ggplot2)
n=1000
data <- rnorm(n) #Error in rpois(n) : argument "lambda" is missing, with no default
dates <- seq(as.Date("2017-05-01"),length=n,by= "days")
ts <- xts(x=data, order.by=dates)
plot(ts)
ggplot(ts)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

We could do it this way:

library(ggplot2)
library(xts)

n <- 1000
data <- rnorm(n)
dates <- seq(as.Date("2017-05-01"), length = n, by = "days")
ts <- xts(x = data, order.by = dates)

# Option1: 
autoplot(ts)


# Option2 using ggplot2
ggplot(df, aes(x = Index, y = data)) + 
  geom_line() +
  labs(x = "Date", y = "Value")

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading