我的数据框是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/