MMF
MMF

Reputation: 3

x axis label in plot overlaps

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"))

Upvotes: 0

Views: 1308

Answers (1)

Mehdi Nellen
Mehdi Nellen

Reputation: 8964

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"))

Upvotes: 1

Related Questions