以R刻度限制以编程方式显示日期范围

时间:2017-07-19 22:34:47

标签: r ggplot2

目前正在使用ggplot2并进行缩放,但最好显示日期范围+/- 1年(例如)。我不应该硬编码这些日期,因为它效率不高。

library(scales) #date time scales
library(ggplot2) # Visualization

ggplot(dataset,aes(x=datetime_start, y=dataset$Product, color=Stage, order = - as.numeric(Stage))) +
geom_segment(aes(x=From,xend=To,yend=dataset$Product), size=10) +
scale_x_datetime(
breaks = date_breaks("1 month"), 
labels=date_format("%b%y"),
limits = c(
      as.POSIXct("2016-03-01"),
      as.POSIXct("2018-02-01")
) 
) +

1 个答案:

答案 0 :(得分:1)

扩大比例:

library(ggplot2)
df <- data.frame(x = seq(Sys.Date()-lubridate::years(2), Sys.Date(), by="3 month")) 
df$y <- 1:nrow(df)
p <- ggplot(df, aes(x, y)) + geom_line()
p + scale_x_date(expand = c(0, 365))
相关问题