Tick ​​date in ggplot2 shows invalid labels

I am showing time series data with ggplot2, but label tags show strange behavior. I’m probably doing something wrong, but I could not find any help on the Internet. here is an example:

#just sample data
time <- as.Date(seq(as.Date("2004/1/1"), as.Date("2009/12/1"), by = "1 month"))
data <- rnorm(nrow(test))+c(1:nrow(test))
test <- data.frame(time, data)

i graph with:

q1 <- ggplot(data=test) + geom_line(aes(x=time, y=data))
q1 <- q1 + scale_x_date(major="years", minor="3 months", format="%Y-%m", lim=c(as.Date("2004/1/1"),as.Date("2009/12/1")), name="") 
q1

this gives the following graph: example graph

but from my understanding the grid should end 2009/12/1 - right? Many thanks for your help!

+5
source share
2 answers

limits scale_x_date , , . http://had.co.nz/ggplot2/scale_date.html ( .)

, coord_cartesian

library(ggplot2)

x <- as.Date(seq(as.Date("2004/1/1"), as.Date("2009/12/1"), by = "1 month"))
y <- rnorm(length(x))+c(1:length(x))
test <- data.frame(time=x, data=y)

q2 <- ggplot(data=test) + 
      geom_line(aes(x=time, y=data)) +
      scale_x_date(major="years", minor="3 months", format="%Y-%m", name="") +
      coord_cartesian(xlim=c(as.Date("2004/1/1"),as.Date("2009/12/1")))

png("date_ticks_plot.png", height=600, width=600)
print(q2)
dev.off()

enter image description here

+8

2009/12/1, , , ggplot, x.

+2

All Articles