Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

r - set date range in ggplot

my data frame is z:

library(ggplot2); library(scales)
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"))

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...