【问题标题】:date axis with fixed limit for barplot and/or hist具有固定条形图和/或历史记录限制的日期轴
【发布时间】:2011-11-06 10:41:47
【问题描述】:

给定日期列表

dates <- data.frame(foo = c( 
           "2009-03-15", 
           "2010-04-15", 
           "2011-06-16", 
           "2011-06-17", 
           "2011-06-17", 
           "2011-06-17", 
           "2011-06-17"))

我可以使用以下命令轻松制作直方图:

histo <- hist(as.Date(dates$foo), breaks = "months", freq=TRUE, plot=TRUE)

我也可以做条形图

barplot(histo$counts)

我的问题:

  • 如何创建时间固定的 x 轴。假设从 2001-02-03 开始​​,到 2011-12-13 结束?
  • 如何在 x 轴上仅添加年份标签,并带有年份刻度线。

注意:我想要一个月度直方图,因此如果可能,建议的解决方案必须保留 breaks = months 或等效值。

【问题讨论】:

    标签: r statistics scale histogram time-series


    【解决方案1】:

    我懒得弄清楚如何使用基本图形来做到这一点。不过在ggplot2 中这很容易:

    library(ggplot2)
    library(zoo)
    ggplot(data = dates,aes(x = as.Date(as.yearmon(foo)))) + 
        geom_bar() + 
        xlim(as.Date(c('2001-01-01','2011-07-20')))
    

    请注意,您所描述的绝不是直方图,而是条形图。从 yearmon 来回转换,然后再转换回 Date 可以让您按月进行分箱,但可以轻松保持日期刻度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 2016-11-26
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      • 2019-03-11
      相关资源
      最近更新 更多