我有数据,其中有儿童的访问日期。
date
16.08.13
16.08.13
16.08.13
17.08.13
27.08.13
03.09.13
04.09.13
05.09.13
07.09.13
07.09.13
我想在R中绘制一个时间序列图,显示日期和相应的访问次数。例如,上面有16个孩子,于2013年8月16日。
另外,我的数据涵盖了3年。所以,我希望看到3年来的季节性变化。
答案 0 :(得分:2)
首先让我们创建一个名为r
的更长的数据集。使用table
计算频率,转换为动物园时间序列和绘图。然后计算每年/每月的平均值并创建monthplot
。最后绘制所有月份与月份的平均值。
# test data
set.seed(123)
r <- as.Date("2000-01-01") + cumsum(rpois(1000, 1))
library(zoo)
opar <- par(mfrow = c(2,2)) # create a 2x2 grid of plots - optional
# plot freq vs. time
tab <- table(r)
z <- zoo(c(tab), as.Date(names(tab)))
plot(z) # this will be the upper left plot
# plot each month separately
zm <- aggregate(z, as.yearmon, mean)
monthplot(zm) # upper right plot
# plot month means
# zc <- aggregate(zm, cycle(zm), mean) # alternative but not equivalent
zc <- aggregate(z, cycle(as.yearmon(time(z))), mean)
plot(zc) # lower plot
par(opar) # reset grid
注意:每年/每月z的总和是zym,所有1月份月份的平均值,所有2月份月份,....,所有12月份的月份是:< / p>
zym <- aggregate(z, as.yearmon(time(z)), sum)
aggregate(zym, cycle(as.yearmon(time(zym))), mean)
答案 1 :(得分:1)
使用ggplot和scale包你可以尝试这样的东西(这是我的代码实际上有效):
<div id="background-wrapper">
<h2>About:</h2>
<h3>We invite you to enjoy a luxurious ground transportation service provided by our team of experts. We have the experience and skills to meet the expectations of every passenger and add value to every ride.</h3>
</div>
答案 2 :(得分:1)
我假设您已熟悉R中的基本数据操作。
实现目标的一种方法是将日期向量制表并创建正确的时间序列对象或data.frame
df <- as.data.frame(table(date)) ### tabulate
df$date <- as.Date(df$date, "%d.%m.%y") ### turn your date to Date class
df
## date Freq
## 1 2013-09-03 1
## 2 2013-09-04 1
## 3 2013-09-05 1
## 4 2013-09-07 2
## 5 2013-08-16 3
## 6 2013-08-17 1
## 7 2013-08-27 1
plot(Freq ~ date, data = df, pch = 19) ### plot
答案 3 :(得分:0)
到目前为止,我们仍然缺少OP要求的季节性趋势分析。我认为这是问题中较为困难的一部分。
如果您的数据仅涵盖3年,您可以通过简单查看每月平均每日访问量来观察季节性变化。
根据您的需求,您可以使用简单的月度图表,或者您可能需要进一步准备数据以计算季节性的确切趋势。
关于如何计算和绘制每日平均每月访问量(每天至少访问一次)的建议
library(ggplot2)
df<-read.table(text="
16.08.13
16.08.13
16.08.13
17.08.13
27.08.13
03.09.13
04.10.13
05.09.13
07.09.13
07.01.14
03.02.14
04.03.14
04.03.14
04.03.14
15.05.14
15.05.14
15.09.14
20.10.14
20.09.14 ", col.names="date")
df <- as.data.frame(table(df)) #get the frequency count (daily)
df$date <- as.Date(df$df, "%d.%m.%y") # turn your date variable to Date class
df$year<-sapply(df$df,function(x) strptime(x,"%d.%m.%Y")$year+1900) #extract month of the visit
df$month<-sapply(df$df,function(x) strptime(x,"%d.%m.%Y")$mon+1) #extract year of the visit
#plot daily frequency
ggplot(aes(x=date, y=Freq), data = df) +
geom_bar(stat = 'identity', position = 'dodge')+
ggtitle("Daily visits")
#compute monthly average visit per day (for days with at least one visit)
library(dplyr)
df2<-df[,c("year","month","Freq")]%>%
group_by(year,month) %>%
summarise_each(funs(mean=mean(., na.rm=TRUE)))
#recreate a date for the graph
df2$date<-as.Date(paste(rep("01",length(df2)),df2$month,df2$year),"%d %m %y")
ggplot(aes(x=date, y=Freq), data = df2) +
geom_bar(stat = 'identity', position = 'dodge')+
ggtitle("Average daily visits per month")