Reputation: 824
first time poster, long time lurker on the R questions...
I've finally been stumped for 48 hours, and I'm coming to all of you beat; but hopefully not for long!
I have an irregular time series that I'm trying to plot with ggplot 2. I would like for the breaks and labels to show only for the days where we have data.
The variable holding the date starts off as a factor read in from excel which I convert to class date:
Dataset[,BatchDateCol] <- as.Date(Dataset[,BatchDateCol],format="%m/%d/%y")
(this is part of a larger system, so I can't just read in the data differently)
I create a vector of the labels in the format I want:
Date_Vec <- c(history[,BatchDateCol])
Date_Vec <- format(Date_Vec, "%b %d")
I then aggregate around the days to get the means for each day:
history <- ddply(Dataset, BatchDateCol, function(z) colMeans(z[RawLocation]))
Now I want to plot it, to do that I find the column with the date, and the variable of interest:
ProductionDate <- grep(BatchDateCol,colnames(history))
Location <- grep(GGVar, colnames(history))
This is where the problem starts; I can create my plot just fine like this:
plot2 <- ggplot(history, aes_string(x=history[ProductionDate], y=history[(Location)]))
plot2 + xlab(XAxisName) +ylab(GGVar)+geom_line(aes_string(y=means), linetype="dashed")
plot2 + geom_point() + geom_smooth(method="lm", formula=y~poly(x,1))
but when I try to add dates to the x axis I get errors, no plot, or no breaks+labels. These 2 commands draw the right plot, but with no breaks or labels:
scale_x_continuous(labels=Date_Vec, breaks=c(1:length(history[,BatchDateCol])),
expand=c(.01,0))
&
scale_x_continuous(labels=Date_Vec, breaks=c(1:length(history[,BatchDateCol])),
limits=c(min(as.numeric(history[,BatchDateCol])),
max(as.numeric(history[,BatchDateCol]))))
This command draws the right breaks and labels but no plot(...!)
scale_x_continuous(labels=Date_Vec,
limits=c(1,length(history[,BatchDateCol])), expand=c(.01,0))
and when I plot it with only:
scale_x_continuous(labels=Date_Vec, expand=c(.01,0))
it sometimes works, but most of the time I get:
Error in scale_labels.continuous(scale, major) : Breaks and labels are different lengths
If I don't specify labels in scale_x_continuous, I get the numeric form of the dates (days since 1970 or whatever) that I want (though I'm not sure its plotting it in the right spot), but I can't figure out how to modify that either.
Finally I've tried changing scale_x_continuous to scale_x_date:
plot2 + scale_x_date(expand=c(.01,0))
which returns an error: (I've tried placing a few different parameters in the ())
Error : Invalid input: date_trans works with objects of class Date only
I have tried leaving Dataset[,BatchDateCol] as a factor or character vector which also does not work.
SO.... I'm at a total loss and feel unbelievably defeated :(
EDIT
ProductionDate is a quoted variable defined here:
QC_Process <- function(Dataset, GGVar, XAxisName="TIME SERIES", BatchDateCol, BatchNum=-1, startdate=NULL, enddate=NULL) {...
So I can't use $ to access the var. Also, I already apply unique right after the var is defined (`history <- unique(history)')
(This is production code for a big R Shiny app, and belongs to my employer, so I can't post to much of it... )
When I use breaks=(history[,ProductionDate])
I get:
Error in as.Date.numeric(value) : 'origin' must be supplied
and When I use 'breaks=(history[ProductionDate])' I get:
Error in is.finite(x) : default method not implemented for type 'list'
print(history[ProductionDate])
returns:
Date.of.Consumption
1 2012-03-24
2 2013-03-11
3 2013-05-10
4 2013-05-11
5 2013-05-13
6 2013-05-16
Upvotes: 2
Views: 2789
Reputation: 824
Got it to work by setting
scale_x_continuous(labels=Date_Vec, Breaks=(as.numeric(history[,ProductionDate])))
Still not sure why it works, and the labels sometimes overlap when I have consecutive dates, but it is progress!
Upvotes: 1
Reputation: 16627
scale_x_continuous(breaks=unique(history$ProductionDate))
should do it (but I can't quite replicate your code, because I don't have the original Dataset
, so I haven't been able to test this solution.
Upvotes: 3