【问题标题】:ggplot: conflict between date_breaks() and limitsggplot:date_breaks() 和限制之间的冲突
【发布时间】:2015-03-23 14:23:58
【问题描述】:

我想每 3 个月获取一次 x 轴上的日期,因此 使用date_breaks("3 month"),但周期从 1/03 开始​​,并且 希望他们去 1/1、1/4 等。这就是我尝试的:

datos <- data.frame(fecha= date_decimal(seq(2013.0,2013.99,by=0.01)),val=runif(100,1,2))
fini <- ymd("20130101")
ffin <- ymd("20131231")
ggplot(data=datos) + geom_point(aes(x=fecha,y=val)) +
  scale_x_datetime(breaks = date_breaks("3 month"),
                   limits=c(fini,ffin),
                   labels = date_format("%d-%m-%Y"))

也试过了:

ggplot(data=datos) + geom_point(aes(x=fecha,y=val)) +
scale_x_datetime(breaks = date_breaks("3 month"),
                 labels = date_format("%d-%m-%Y")) +
xlim(c(fini,ffin))

然后用首字母缩略词和需要数字来获取月份(看起来 xlim() 取消了之前的 scale_x_datetime() )

【问题讨论】:

    标签: r ggplot2 timeserieschart


    【解决方案1】:

    这应该可行:

    library(ggplot2)
    library(scales)
    library(lubridate)
    datos <- data.frame(fecha= date_decimal(seq(2013.0,2013.99,by=0.01)),val=runif(100,1,2))
    fini <- ymd("20130101")
    ffin <- ymd("20140101")
    
    datebreaks <- seq(as.Date(fini), as.Date(ffin), by="3 month")
    
    ggplot(data=datos) + 
      geom_point(aes(x = as.Date(fecha), y = val)) +
      scale_x_date(breaks = datebreaks,
                   limits = c(as.Date(fini), as.Date(ffin)),
                   labels = date_format('%d-%m-%Y')) 
    

    【讨论】:

      猜你喜欢
      • 2012-06-27
      • 2012-03-06
      • 2011-05-02
      • 2012-11-25
      • 2013-10-27
      • 2014-03-02
      • 2016-11-15
      • 2012-11-02
      • 2021-02-22
      相关资源
      最近更新 更多