【问题标题】:Formatting histogram x-axis when working with dates using R使用 R 处理日期时格式化直方图 x 轴
【发布时间】:2012-08-23 01:44:53
【问题描述】:

我正在使用 R 创建流行病曲线(每天疾病病例数的直方图),并且在格式化 x 轴方面有点挣扎。

我知道 ggplot 提供了非常漂亮的图形和易于操作的轴 (Understanding dates and plotting a histogram with ggplot2 in R),但在这种情况下,我更喜欢使用 hist() 命令,因为我同时描述了 2 种不同的模式,如下所示(我认为你不能在 ggplot 中做类似的事情):

这里的问题是 x 轴不是从第一种情况开始的,有太多的刻度线,我希望能够有一个系统的日期标记,例如。每 7 天或每月 1 日。

数据以每个疑似病例一行的形式存储在数据库 (dat.geo) 中,包括发病日期和郊区信息(直方图中的黑色或白色),如下所示:

> head(dat.geo)
  number age sex       suburb Date_of_Onset
1      1  12   F            x    2011-10-11
2      2  28   M            x    2011-10-10
3      3  15   F            x    2011-10-12
4      4  12   M            y    2011-10-25
5      5  10   F            x    2011-10-15
6      6   9   M            y    2011-10-20

这是我的代码:

pdf(file='1.epi.curve.pdf')
hist(dat.geo$Date_of_Onset[(dat.geo$suburb=="x")], "days", 
 format = "%d %b %y", freq=T, col=rgb(0,0,0,1), axes=T, main="", add=T)
hist(dat.geo$Date_of_Onset[(dat.geo$suburb=="y")], "days", 
 format = "%d %b %y", freq=T, main="", col=rgb(1,1,1,.6), add=T, axes=F)
dev.off()

我已经尝试抑制轴并稍后使用此代码添加一个操纵轴

axis(1, labels=T)
axis(2)

但这就是我得到的(我不知道如何操作):

非常感谢您的帮助!

谢谢

【问题讨论】:

  • 使用axis(1, at=x, labels=y),其中x 是刻度的坐标(数字向量),y 是刻度标签(字符向量)。
  • 您可以在ggplot 中使用position="identity" 来覆盖条形

标签: r date histogram


【解决方案1】:

由于您有效地要求我们提供ggplot 解决方案,所以这里是:

dates <- seq(as.Date("2011-10-01"), length.out=60, by="+1 day")

set.seed(1)
dat <- data.frame(
  suburb <- rep(LETTERS[24:26], times=c(100, 200, 300)),
  Date_of_Onset <- c(
    sample(dates-30, 100, replace=TRUE),
    sample(dates,    200, replace=TRUE),
    sample(dates+30, 300, replace=TRUE)
  )
)

library(scales)
library(ggplot2)
ggplot(dat, aes(x=Date_of_Onset, fill=suburb)) + 
  stat_bin(binwidth=1, position="identity") + 
  scale_x_date(breaks=date_breaks(width="1 month"))

注意使用position="identity" 来强制每个条形图起源于轴上,否则默认情况下会得到堆叠图。

【讨论】:

  • 感谢您的努力 - 看起来确实不错,但我更喜欢针对这个特定问题的原始解决方案,因为您可以看到两条曲线的完整。不过,我会记住这项技术以备将来使用
  • @jpolonsky 你能解释一下你能看到两条曲线的全部是什么意思吗?你建议堆叠酒吧吗?或者躲避酒吧?或者是其他东西?在ggplot 中也可以使用这些选项中的任何一个。
  • 抱歉,我刚刚看到通过在 stat_bin 命令中添加 alpha=0.5 可以得到透明胶片,这正是我所追求的。感谢这个出色的解决方案!
  • @jpolonsky 很高兴能帮上忙!
  • 感谢您的建议 - 我找到了问题; 'dates' 和 'scales' 包之间似乎存在交互。两者都加载后,秤不起作用,但没有加载“日期”,它就可以正常工作。
【解决方案2】:

有 2 种可用的解决方案; 1个使用 hist() 另一个使用 ggplot():

library(date)
hist(dat.geo$Date_of_Onset[(dat.geo$suburb=="x")], "weeks", 
 format = "%d %b %y", freq=T, col=rgb(0,0,0,1), axes=F, main="")
hist(dat.geo$Date_of_Onset[(dat.geo$suburb=="y")], "weeks", 
 format = "%d %b %y", freq=T, main="", col=rgb(1,1,1,.6), add=T, axes=F)
axis.Date(1, at=seq(as.Date("2011-10-10"), as.Date("2012-03-19"), by="2 weeks"),
 format="%d %b %y")
axis.Date(1, at=seq(as.Date("2011-10-10"), as.Date("2012-03-19"), by="weeks"), 
 labels=F, tcl= -0.5)

这个流行曲线如下:

上面 Andrie 建议的使用 ggplot 的解决方案如下:

library(scales)
library(ggplot2)
ggplot(dat.geo,aes(x=Date_of_Onset, group=suburb, fill=suburb))+
 stat_bin(colour="black", binwidth=1, alpha=0.5,
 position="identity") + theme_bw()+
 xlab("Date of onset of symptoms")+
 ylab("Number of cases")+
 scale_x_date(breaks=date_breaks("1 month"), labels=date_format("%b %y"))

给出如下流行曲线:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 2016-02-29
    • 2014-06-06
    • 2014-08-12
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多