R 使用每日数据从数据框绘制月度时间序列
我有一套2014年1月1日至2012年12月31日期间纽约每天发生的机动车碰撞数据。我想在一个单独的图中绘制每月受伤骑车人和驾车人数量的时间序列 我的数据如下所示:R 使用每日数据从数据框绘制月度时间序列,r,plot,time-series,legend,R,Plot,Time Series,Legend,我有一套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
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)
cyclistMannat这里有一个答案,使用data.table
package帮助您汇总。使用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)
阴谋
使用ggplot2
软件包
或有关绘图,请参阅以获取绘图帮助
# 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"
#我没有您的完整数据,因此单点折线图不起作用
#我还需要一个月的测试,所以加了一个假二月
testfebMannat这里是一个使用data.table
package帮助您聚合的答案。使用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)
阴谋
使用ggplot2
软件包
或有关绘图,请参阅以获取绘图帮助
# 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"
#我没有您的完整数据,因此单点折线图不起作用
#我还需要一个月的测试,所以加了一个假二月
testfeb尝试%Y
而不是%Y
。不,它仍然给出相同的错误结果。我不这么认为<代码>截止日期(“2014年1月1日”,%m/%d/%Y”)
工作正常。请更具体一些。(1) 展示错误的结果,你是如何得到的,以及你的期望。(2) 以可复制的形式提供数据,例如,dput(head(final_data))的输出。
(3)问题要求提供行人应变时间序列,但数据框中没有行人数据。(4) 您是否希望按日期对每个数字列求和,然后根据日期绘制和,而忽略时间和位置列?Mannat,您需要一个新字段,该字段只包含月份数据,如Jan,然后可以在该字段上进行聚合。请参见下面我的答案,其中我创建了一个PlotDate来帮助您尝试%Y
而不是%Y
。不,它仍然会给出相同的错误结果。我不这么认为<代码>截止日期(“2014年1月1日”,%m/%d/%Y”)
工作正常。请更具体一些。(1) 展示错误的结果,你是如何得到的,以及你的期望。(2) 以可复制的形式提供数据,例如,dput(head(final_data))的输出。
(3)问题要求提供行人应变时间序列,但数据框中没有行人数据。(4) 您是否希望按日期对每个数字列求和,然后根据日期绘制和,而忽略时间和位置列?Mannat,您需要一个新字段,该字段只包含月份数据,如Jan,然后可以在该字段上进行聚合。请参阅下面我的答案,其中我创建了一个PlotDate来帮助您完成此任务。谢谢您的帮助。但是我遇到了以下错误:[.data.frame
中的错误(最终数据,:=
(PlotDate,as.numeric(format(DATE,:找不到函数):=“1)是否安装了.packages(data.table)?2)需要使用库命令3加载data.table)你需要将你的数据框转换成数据。表,例如,最终数据。我怎样才能使它成为一条不同颜色的线而不是点。?嘿,Micstr,我从这里看到,我们可以分别绘制每个pf em的曲线图。但是,我希望在同一个曲线图中绘制每个pf em的月度时间序列。你能帮忙吗?请看?绘制到理解绘图功能。要进行线条绘图(x,y,type=“l”,col=“red”)
谢谢您的帮助。但是我遇到了以下错误:[.data.frame
(最终数据,:=
(绘图日期,如.numeric(格式(日期::找不到函数):=“1)是否安装了.packages(数据表)?2)您需要使用库命令3)加载data.table你需要将你的数据框转换成数据。表,例如,最终数据。我怎样才能使它成为一条不同颜色的线而不是点。?嘿,Micstr,我从这里看到,我们可以分别绘制每个pf em的曲线图。但是,我希望在同一个曲线图中绘制每个pf em的月度时间序列。你能帮忙吗?请看?绘制到了解绘图功能。进行线条绘图(x,y,type=“l”,col=“red”)