簡體   English   中英

如何在R中以日期格式繪制數據的時間序列圖

[英]How to draw time series plot for data in date format in R

我有數據,其中有兒童探望的日期。

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中繪制一個時間序列圖,以顯示日期和相應的訪問次數。 例如,上面有2013年8月16日有3個孩子。

另外,我的數據涵蓋了3年。 因此,我希望看到3年內的季節性變化。

首先讓我們創建一個更長的數據集,稱為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,所有一月,二月,...,十二月的平均值為:

zym <- aggregate(z, as.yearmon(time(z)), sum)
aggregate(zym, cycle(as.yearmon(time(zym))), mean)

使用ggplot和scale包,您可以嘗試這樣的操作(這是我的代碼中的一部分,實際有效):

library(ggplot2)
library(lubridate)
library(scales)

g_sm_ddply <- ggplot(final_data, aes(x = as.Date(dates), y = scon_me, fill = tipo))
g_sm_ddply + geom_bar(position = "dodge", stat = "identity") +
    labs(title = "SCONTRINO MEDIO ACQ_ISS_KPMG NUOVA CLUSTERIZZAZIONE", x = "data", y = "scontrino medio")+
    scale_x_date(breaks = date_breaks("month"), labels = date_format("%Y/%m"))

我假設您已經熟悉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

在此處輸入圖片說明

到目前為止,我們仍然缺少OP所要求的季節性趨勢分析。 我認為這是問題中比較困難的部分。

如果您的數據僅涵蓋3年,則可以通過簡單地查看每月平均每日訪問量來觀察季節變化。

根據您的需求,您可以使用簡單的月度圖,或者可能需要進一步准備數據以計算季節性的確切趨勢。

下面是關於如何計算和繪制每日平均每月訪問量(每天至少訪問1次)的建議

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM