簡體   English   中英

從具有每日數據的數據框中繪制每月時間序列

[英]Plot monthly Time series from a data frame with daily data

我有一個數據集,用於2014年1月1日至2012年12月31日在紐約市每天發生的機動車撞車事故。我想繪制一個月中每個騎自行車者和駕駛者受傷人數的時間序列。

我的數據如下所示:

    Date      Time   Location   Cyclists injured  Motorists injured
2014-1-1     12:05      Bronx                  0                  1
2014-1-1     12:34      Bronx                  1                  2
2014-1-2      6:05      Bronx                  0                  0
2014-1-3      8:01      Bronx                  1                  2
2014-1-3     12:05  Manhattan                  0                  1
2014-1-3     12:56  Manhattan                  0                  2

依此類推,直到2014年12月31日。

現在,要為此繪制每月時間序列,我知道我首先需要將每個月的每個總計加起來,然后繪制每月總計。 但是我不知道該怎么做。

我使用了此代碼的聚合函數,但是它給了我每一天而不是一個月的總和。 請幫忙。

cyclist <- aggregate(NUMBER.OF.CYCLIST.INJURED ~ DATE, data = final_data,sum)

謝謝 :)

Mannat在這里是使用data.table包來幫助您匯總的答案。 使用install.packages(data.table)首先將其放入R。

library(data.table)

# For others
#   I copied your data into a csv file, Mannat you will not need this step,
#   other helpers look at data in DATA section below 
final_data <- as.data.table(read.csv(file.path(mypath, "SOaccidents.csv"),
                                     header = TRUE,
                                     stringsAsFactors = FALSE))
# For Mannat
# Mannat you will need to convert your existing data.frame to data.table
final_data <- as.data.table(final_data)

# check data formats, dates are strings 
# and field is Date not DATE
str(final_data)

final_data$Date <- as.Date(final_data$Date, "%m/%d/%Y")

# use data table to aggregate on months 
# First lets add a field plot date with Year and Month YYYYMM 201401
final_data[, PlotDate := as.numeric(format(Date, "%Y%m"))] 

# key by this plot date
setkeyv(final_data, "PlotDate")

# second we aggregate with by , and label columns
plotdata <- final_data[, .(Cyclists.monthly  = sum(Cyclists.injured), 
                           Motorists.monthly = sum(Motorists.injured)), by = PlotDate]

#   PlotDate Cyclists.monthly Motorists.monthly
#1:   201401                2                 8

# You can then plot this (makes more sense with more data)
# for example, for cyclists
plot(plotdata$PlotDate, plotdata$Cyclists.monthly)

Mannat如果你不熟悉data.table ,請參閱的cheatsheet

數據

對於其他希望為此工作的人。 這是dput的結果:

final_data <- data.table(Date = c("01/01/2014", "01/01/2014", "01/01/2014", 
                        "01/01/2014", "1/19/2014", "1/19/2014"), 
                        Time = c("12:05", "12:34","06:05", "08:01", "12:05", "12:56"),
                        Location = c("Bronx", "Bronx","Bronx", "Bronx", 
                            "Manhattan", "Manhattan"),
                        Cyclists.injured = c(0L, 1L, 0L, 1L, 0L, 0L),
                        Motorists.injured = c(1L, 2L, 0L, 2L, 1L, 2L))

情節

要么使用ggplot2

或有關繪圖,請參閱在R中繪制多條線(數據系列),每條線具有唯一的顏色以獲取繪圖幫助。

# I do not have your full data so one point line charts not working
# I needed another month for testing, so added a fake February
testfeb <- data.table(PlotDate = 201402, Cyclists.monthly = 4,
                      Motorists.monthly = 10)
plotdata <- rbindlist(list(plotdata, testfeb))

# PlotDate  Cyclists.monthly    Motorists.monthly
#1  201401                 2                    8
#2  201402                 4                   10

# Plot code, modify the limits as you see fit
plot(1, type = "n",
     xlim = c(201401,201412), 
     ylim = c(0, max(plotdata$Motorists.monthly)),
     ylab = 'monthly accidents',
     xlab = 'months')

lines(plotdata$PlotDate, plotdata$Motorists.monthly, col = "blue")
lines(plotdata$PlotDate, plotdata$Cyclists.monthly, col = "red")

# to add legend
legend(x = "topright", legend = c("Motorists","Cyclists"),
       lty=c(1,1,1), lwd=c(2.5,2.5,2.5), 
       col=c("blue", "red"))
# or set legend inset x to another position e.g. "bottom" or "bottomleft"

圖例的事故情節示例

暫無
暫無

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

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