图中的 x 轴标签重叠 [英] x axis label in plot overlaps

查看:62
本文介绍了图中的 x 轴标签重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 2004 年 1 月至 2014 年 12 月期间淡化了购物"一词的搜索量时间序列图.但是,我的 x 轴上的日期显示了我的数据所拥有的所有月份,并且它们重叠看起来很乱.如何将其转换为仅显示的年份?

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?

这是我的代码:

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():

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

这篇关于图中的 x 轴标签重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆