Quick disclaymer: In the end i think ggplot
is easier. I'm going to try explaining in a way you can generalize it, which can make it seem complicated, but it's not that hard. Also, i'm no genius at autoplot, so maybe there is an easier way that i don't know of. Lastly, i use "y" for the time series column, and "date" for the dates.
Reading your date as an date object is nice even for the 'simpler' autoplot approach, and it's not hard:
format = "%Y-%m-%d %H:%M:S"
df$date = as.POSIXct(df$date, format, tz="your time zone code here")
Values for the limits
d = which(df$date=="2019-10-01 00:00:00") #First date you want
e = which(df$date=="2019-12-01 00:00:00") #Last date you want
Values for the x axis breaks. Now you want to apply your breaks only to the data in the limits, so remember that when setting a and n.
a = 1 #The date you wish to start the ticks. If you wanted to be the 1st of oct, for example:
a = which(df$date=="2019-10-01 00:00:00")
n = 12 #The number of months there will be in the ticks
k = 720 #The conversion factor, in this case is months-->hours
autoplot(df$y) + xlim(d, e) +
scale_x_continuous(breaks=seq(a,n*k,k),
labels=months(df$date,TRUE))
#Set FALSE to not use abbreviations, Set labels=1:n to use numbers
As you can see, because you don't pass df$date as an argument to autoplot, you have to "chew" the info about breaks and limits to it, which you don't need in ggplot. You don't need to understand every option you have with ggplot if you don't want for now, you can try to memorize this structure:
ggplot(df,aes(x=date,y=y)) + #Pass the data frame, then the x and y column names inside "aes()"
geom_line() + #For time series, you'll probably always want a line graph
scale_x_date(breaks="1 month", labels="%m", #Set labels=month(date,TRUE/FALSE) to get month names
limits=as.POSIXct(c("first date", "last date"), format))
Instead defining a,b,c and creating a sequence to breaks, we just say "1 month", and instead looking for where the limit dates are in our df, we just say when are they. It's also easier to change the scale, if you want to do weekly, just change breaks
to "1 week"
and labels to %W
, whereas with autoplot you need to recalculate a,n,k
. Sorry if it seemed complicated again.