在ggplot中设置日期范围 [英] set date range in ggplot

查看:179
本文介绍了在ggplot中设置日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框是z:

> dput(z)
structure(list(Month = structure(c(14975, 15095, 15156, 15187, 
15248), class = "Date"), Value = c(1, 1, 1, 6, 1)), .Names = c("Month", 
"Value"), row.names = c(NA, 5L), class = "data.frame")


ggplot(z, aes(Month, Value)) + 
    geom_bar(fill="orange",size=.3,  stat="identity", position="identity") +
    geom_smooth(data=z,aes(Month,Value,group=1), method="lm", size=2, color="navyblue") + 
    scale_x_date(breaks = "1 month", labels=date_format("%b-%Y"))

这可以正常工作,但我真的很喜欢我的数据范围2011年1月1日至2011年1月1日。我的示例日期是从1/12011到10/1/2011。有没有一个简单的方法强制日期范围从1/1/2011到1/1/2013在ggplot?

This works ok but I really like my data range between 1/1/2011 and 1/1/2013. My example date is from 1/12011 to 10/1/2011. Is there an easy way to force the date range from 1/1/2011 to 1/1/2013 in ggplot?

推荐答案

?scale_x_date 中的文档提到它接受所有典型连续规模论证,包括限制

The documentation at ?scale_x_date mentions that it accepts all "typical" continuous scale arguments, including limits:

library(scales)
ggplot(z, aes(Month, Value)) + 
    geom_bar(fill="orange",size=.3,  stat="identity", position="identity") + 
    geom_smooth(data=z,aes(Month,Value,group=1), method="lm", size=2, color="navyblue") + 
    scale_x_date(date_breaks = "1 month", 
                 labels=date_format("%b-%Y"),
                 limits = as.Date(c('2011-01-01','2013-01-01')))

这篇关于在ggplot中设置日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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