R ggplot:时间序列bbar图表具有领先和滞后的空间 [英] R ggplot: time series bbar chart has leading and lagging spaces

查看:247
本文介绍了R ggplot:时间序列bbar图表具有领先和滞后的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,你们帮助我将我的图表的x轴格式化为日期轴:



如果您想要每两年更换一次标签:

  dates_for_breaks<  -  c(seq .Date(from = min(dates_vec),
to = max(dates_vec),by =2 years),max(dates_vec))


Recently, you guys helped me format the x-axis of my chart as a date axis:

R ggplot2: bar chart of a time series

My data spans from July 2006 to June 2016 but R is automatically making the scale of the x-axis include all of 2006 and all of 2016. This results in 'blank space' on either end of the chart. I would also like for the labels to have Jul 20XX instead of every December. I tried forcing it to fit my data with the command:

scale_x_date(limits = c(min, max), breaks=date_breaks("12 months"), labels=date_format("%b %Y")) +

but it that doesn't seem to change anything except it reformatted my labels as Dec 20XX.

解决方案

Assuming g as the plot object from the answer posted to the liked question:

dates_vec <- as.Date(rownames(sample), "%m/%d/%Y")
dates_for_breaks <- c(seq.Date(from = min(dates_vec), 
                                   to = max(dates_vec), by = "year"), max(dates_vec))

g + scale_x_date(breaks = dates_for_breaks, date_labels = "%b %Y", expand = c(0, 0)) 

gives

If you want the labels every 2 years:

dates_for_breaks <- c(seq.Date(from = min(dates_vec), 
                                   to = max(dates_vec), by = "2 years"), max(dates_vec))

这篇关于R ggplot:时间序列bbar图表具有领先和滞后的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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