Anand
Anand

Reputation: 769

trouble getting Date field on X axis using ggplot2

head(bktst.plotdata)   
   date      method  product type  actuals  forecast   residual   Percent_error month
1 2012-12-31  bauwd   CUSTM  NET  194727.51  -8192.00 -202919.51       -104.21 Dec12
2 2013-01-31  bauwd   CUSTM  NET  470416.27   1272.01 -469144.26        -99.73 Jan13
3 2013-02-28  bauwd   CUSTM  NET  190943.57  -1892.45 -192836.02       -100.99 Feb13
4 2013-03-31  bauwd   CUSTM  NET  -42908.91   2560.05   45468.96       -105.97 Mar13
5 2013-04-30  bauwd   CUSTM  NET -102401.68 358807.48  461209.16       -450.39 Apr13
6 2013-05-31  bauwd   CUSTM  NET -134869.73 337325.33  472195.06       -350.11 May13

I have been trying to plot my back test result using ggplot2. Given above a sample dataset. I have dates ranging from Dec2012 to Jul2013. 3 levels in 'method', 5 levels in 'product' and 2 levels in 'type' I tried this code, trouble is that R is not reading x-axis correct, on the X-axis I am getting 'Jan, feb, mar, apr, may,jun, jul, aug', instead I expect R to plot Dec-to-Jul

month.plot1 <- ggplot(data=bktst.plotdata, aes(x= date, y=Percent_error, colour=method))
facet4 <- facet_grid(product~type,scales="free_y")
title3 <- ggtitle("Percent Error - Month-over-Month")
xaxis2 <- xlab("Date")
yaxis3 <- ylab("Error (%)")
month.plot1+geom_line(stat="identity", size=1, position="identity")+facet4+title3+xaxis2+yaxis3

# Tried changing the code to this still not getting the X-axis right
month.plot1 <- ggplot(data=bktst.plotdata, aes(x= format(date,'%b%y'), y=Percent_error, colour=method))
month.plot1+geom_line(stat="identity", size=1, position="identity")+facet4+title3+xaxis2+yaxis3

Upvotes: 0

Views: 700

Answers (1)

aosmith
aosmith

Reputation: 36104

Well, it looks like you are plotting the last day of each month, so it actually makes sense to me that December 31 is plotted very very close to January. If you look at the plotted points (with geom_point) you can see that each point is just to the left of the closest month axis.

It sounds like you want to plot years and months instead of actual dates. There are a variety of ways you might do this, but one thing you could is to change the day part of the date to the first of the month instead of the last of the month. Here I show how you could do this using some functions from package lubridate along with paste (I have assumed your variable date is already a Date object).

require(lubridate)
bktst.plotdata$date2 = as.Date(with(bktst.plotdata, 
                                    paste(year(date), month(date), "01", sep = "-")))

Then the plot axes start at December. You can change the format of the x axis if you load the scales package.

require(scales)
ggplot(data=bktst.plotdata, aes(x = date2, y=Percent_error, colour=method)) +
    facet_grid(product~type,scales="free_y") +
    ggtitle("Percent Error - Month-over-Month") +
    xlab("Date") + ylab("Error (%)") +
    geom_line() +
    scale_x_date(labels=date_format(format = "%m-%Y"))

Upvotes: 1

Related Questions