0
votes

I plot a time series graph of search volume on the word "shopping" which is deseasonalized over the period of January 2004 to December 2014. However the dates on my x axis shows all the months I have for my data and it overlaps which looks messy. How can I convert this into only the years that are shown?

This is my code:

ggplot(data=mergeddata, aes(x=Date, y=ShoppingDeseason, group=1)) + 
  geom_line(colour="red", size=1) + xlab("Date [Year]") + 
  ylab("ShoppingDeseason [SearchVolume]") +
  ggtitle("Search Volume on Shopping from 2004 to 2014") + 
  theme(axis.text.x=element_text(size=20,angle=90, face="bold"),
        axis.text.y=element_text(size=20, face="bold"),
        axis.title=element_text(size=22,face="bold"))
1
Hey MMF, welcome! You forgot to add your data, which helps us to reproduce your problem. Read the Tour and you will earn your first badge ;) - Mehdi Nellen
I got this Error: Invalid input: date_trans works with objects of class Date only. My dates are only years and months eg: "2004-01" there are no days. is it because of this? - MMF
mergeddata$Date <- as.Date(paste(mergeddata$Date,"-01",sep="")) add this before you start with your ggplot. It will convert your Date column into dates - Mehdi Nellen

1 Answers

1
votes

Try to use scale_x_date():

# Generate Random data
mergeddata <- data.frame(Date = seq(as.Date('2004-01-01'),as.Date('2014-12-31'),by = "month"),
                         ShoppingDeseason = sample(20, 132, replace=TRUE))

ggplot(data=mergeddata, aes(x=Date, y=ShoppingDeseason, group=1)) + 
  geom_line(colour="red", size=1) + xlab("Date [Year]") + 
  ylab("ShoppingDeseason [SearchVolume]") +
  ggtitle("Search Volume on Shopping from 2004 to 2014") + 
  theme(axis.text.x=element_text(size=20,angle=90, face="bold"),
        axis.text.y=element_text(size=20, face="bold"),
        axis.title=element_text(size=22,face="bold")) +
  scale_x_date(labels = date_format("%Y"), breaks = date_breaks("year"))