2

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?

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
Jule
  • 123
  • 9
  • 1
    You need to add a reproducible example i.e. show some data (not all) that produce the blank x-axis that you talk about. – LyzandeR May 30 '15 at 12:31
  • maybe your `at` values are not in the range of the plot – rawr May 30 '15 at 12:49
  • I think the values are disappearing because the dates variable is somehow being scaled wrong. In my answer below I cast it as a Posix date and just use the normal axis function instead and it worked. – Mike Wise May 30 '15 at 14:30

1 Answers1

1

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

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • 1
    thank you! It worked! :) Instead of small single lines for each year (like your graphic has), there is just one big, black bar under my x-axis. Do you know how to get rid of it? – Jule May 30 '15 at 14:48
  • No, but in general if you need more control with your plots you are probably better off learning how to use ggplot2. It is a lot more flexible, but has a higher learning curve. You might want to post the new problem as a new post, but this time give it as a complete example so people can play with it. You will get answers much faster that way. – Mike Wise May 30 '15 at 14:55