Jule
Jule

Reputation: 123

Dates on x-axis, time series

I have data covering a time period of over 25 years and I would like to see the years on the x-axis.

dates <- as.Date(Dollar[,1], "%d.%m.%Y") 
Dollar <- as.xts(Dollar[,2], dates)
plot(SWEDOLall, xaxt = "n", main="SMA", ann = FALSE)
axis.Date(side = 1, dates, at = labDates, format = "%y", labels = TRUE)
title(ylab = "Value")
title(xlab = "Time") 

But my x-axis is just blank. Can anybody see what mistake I have made?

Upvotes: 2

Views: 1769

Answers (1)

Mike Wise
Mike Wise

Reputation: 22807

I had to augment your example to get something to play with, but here is something that works. And I just changed it to eliminate lubridate...

library(xts)
d1 <- seq(as.Date("2001-01-01"),as.Date("2021-01-01"),"years")
d2 <- rnorm(21,10,1)
Dollar <- data.frame(d1,d2)
dates <- as.Date(Dollar[,1], "%d.%m.%Y",tz="GMT") 
xtsplot <- as.xts(Dollar[,2], dates)
plot(xtsplot, xaxt = "n", main="SMA", ann = FALSE)
axis(1, at=as.POSIXct(dates),labels=format(dates,"%Y"))
title(ylab = "Value")
title(xlab = "Time") 

enter image description here

Upvotes: 1

Related Questions