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.