使用R处理日期时格式化直方图x轴 [英] Formatting histogram x-axis when working with dates using R
问题描述
我正在使用R创建一个流行曲线(每天的疾病数量的直方图),并且在使用x轴格式化的同时,还在努力。
I am in the process of creating an epidemic curve (histogram of number of cases of a disease per day) using R, and am struggling a little with formatting the x-axis.
我知道ggplot给出了非常好的图形和易于操作的轴(了解日期并在R中绘制ggplot2的直方图),但在这种情况下,我更喜欢使用 hist()
命令,因为我在同一时间描述2种不同的模式,如下(我不认为你可以在ggplot中做类似的事情):
I am aware that ggplot gives very nice graphs and easily manipulatable axes ( Understanding dates and plotting a histogram with ggplot2 in R ), but in this case I prefer to use the hist()
command, because I am describing 2 different patterns at the same time, as below (I don't think you can do something similar in ggplot):
这里的问题是x轴不从第一种情况开始,有太多的刻度线,我想要有一个系统的日期标记,例如。每7天,或每月1日。
The problem here is that the x-axis does not begin at the first case, has too many tick marks, and I'd like to be able to have a systematic date marker, eg. every 7 days, or every 1st of the month.
数据存储在数据库(dat.geo)中,作为每个疑似病例的一行,其发生日期和郊区的信息(无论是直方图是黑色还是白色) ,如下所示:
The data are stored in a database (dat.geo) as one row per suspected case, with info on date of onset and suburb (whether black or white in histogram), as below:
> 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()
我已经尝试使用这个代码来抑制轴并添加一个被操纵的
I have tried suppressing the axis and adding a manipulated one later using this code
axis(1, labels=T)
axis(2)
但这是我得到的(我不知道如何操纵):
but this is what I get (and I have no idea how to manipulate that):
您的帮助非常感谢!
谢谢
推荐答案
由于您有效地挑战我们提供 ggplot
解决方案,这里是:
Since you effectively challenged us to provide a ggplot
solution, here it is:
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
强制每个条形图发生在轴上,否则默认情况下会获得堆叠图表。
Note the use of position="identity"
to force each bar to originate on the axis, otherwise you get a stacked chart by default.
这篇关于使用R处理日期时格式化直方图x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!