r - 在ggplot中设置日期范围

标签 r date ggplot2

我的数据框是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日至2013年1月1日之间的数据范围。我的示例日期是从1/12011到10/1/2011。有没有一种简单的方法可以在ggplot中强制将日期范围从1/1/2011更改为1/1/2013?

最佳答案

?scale_x_date上的文档提到它接受所有“典型”连续刻度参数,包括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')))

关于r - 在ggplot中设置日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14162829/

相关文章:

R 使用 if else 语句从列表中删除对象

r - 在每个面的密度图的中心平均值上画一条线

r - 如何加快将数据加载到 R 中的速度?

java - SQL - 按日期循环行的最佳实践方法

mysql - 使用 bootstrap-datepicker-rails,现在我的日期是字符串,但我需要将其作为日期保存到数据库

c++ - 为什么 date::floor 从 time_t 派生时计算 2 天前的日期

r-ggplot2 : filling the area between two rays

r - 在 ggplot 外添加文本

r - Spark R - Spark R 中的 `factors()` 是什么?

R:在 geom_smooth 中的自定义函数中使用时,nls 不获取其他参数