3

I would like to manually add breaks to a ggplot2 time series graph with dates. Unfortunately, nothing I have tried works. I tried the following suggestion but it seems to no longer work with more recent versions of ggplot2 (as others have commented) Breaks for scale_x_date in ggplot2 and R Here is a reproducible example:

library(gtrends)
library(ggplot2)
library(cowplot)
library(reshape2)

ch <- gconnect(xxx@xxx.com, xxxx)
res<-gtrends(c("NBA"), start_date="2014-01-01")
trend<-(res$trend)
trend.m<-melt(trend, id.var=c("start","end"))
trend.m$date<-as.Date(start)

ggplot(data=trend.m,aes(x= date,y=value,color=variable))+
geom_line(size=0.5) + theme_bw() + 
scale_x_date(date_breaks = "6 month",labels = date_format("%b %y"))

I tried the following:

ggplot(data=trend.m,aes(x=date,y=value))+geom_line(size=0.5) + 
   theme_bw()+scale_x_date(breaks = c("2016-02-12",'2014-11-10'), 
   labels = c("Label 1","Label 2")) 

Which produced the following error: Error in strsplit(unitspec, " ") : non-character argument

I also tried the following

library(scales)

ggplot(data=trend.m,aes(x=date,y=value))+geom_line(size=0.5) + 
   theme_bw()+scale_x_date(date_breaks = c("2016-02-12",'2014-11-10'), 
   labels = c("Label 1","Label 2"))

and

ggplot(data=trend.m,aes(x=date,y=value))+geom_line(size=0.5) + 
       theme_bw()+scale_x_date(date_breaks = c("2016-02-12",'2014-11-10'), 
       date_labels = c("Label 1","Label 2"))

Both of which produced this error:

Error in cut.Date(date, time, right = TRUE, include.lowest = TRUE) : 
  invalid specification of 'breaks'
In addition: Warning message:
In if (prec$unit == "day") { :
  the condition has length > 1 and only the first element will be used

Any help is much appreciated.

Community
  • 1
  • 1
Cyrus Mohammadian
  • 4,982
  • 6
  • 33
  • 62
  • http://stackoverflow.com/questions/32653730/ggplot2-scale-x-date – Gopala Apr 30 '16 at 04:13
  • Yeah i've already tried the library(scales) (hence why i loaded it in the dependencies). That didnt work (produced the following error: ``Error in cut.Date(date, time, right = TRUE, include.lowest = TRUE) : invalid specification of 'breaks' In addition: Warning message: In if (prec$unit == "day") { : the condition has length > 1 and only the first element will be used``)to begin with, which was my default assumption so i checked out the ``?help` for the ``scale_x_date()` function and i followed that suggestion which is what I posted. – Cyrus Mohammadian Apr 30 '16 at 04:18
  • 1
    Try wrapping the date breaks vector in as.Date(). – eipi10 Apr 30 '16 at 04:22
  • Yeah I tried that too and it also gave me an error (``Error in strsplit(unitspec, " ") : non-character argument``). I took the time to make a reproducible example, if you think you may know how to do it, please go ahead and try. I've tried on my own too many different ways to able to exhaustively list them all. – Cyrus Mohammadian Apr 30 '16 at 05:00
  • 1
    You mean this doesn't work: `scale_x_date(breaks = as.Date(c("2016-02-12",'2014-11-10')), labels = c("Label 1","Label 2"))`? This worked fine for me with some fake data. – eipi10 Apr 30 '16 at 05:05
  • Yeah I tried it, but actually not when I had library(scales) loaded, that worked thanks! Post it as an answer so its "resolved" please, and thanks! – Cyrus Mohammadian Apr 30 '16 at 05:07
  • Weird, I don't thnk it has anything to do with library(scales), not sure why it wasn't working before. Thanks again. – Cyrus Mohammadian Apr 30 '16 at 05:10

1 Answers1

4

It should work if you format your breaks as dates rather than as strings. In the code below I've just wrapped the breaks vector in as.Date():

scale_x_date(breaks = as.Date(c("2016-02-12",'2014-11-10')), 
             labels = c("Label 1","Label 2"))
eipi10
  • 91,525
  • 24
  • 209
  • 285