r语言 - 使用日期向量获取直方图



我正在使用R处理此数据集,并尝试显示每年的事件数量。

使用R的帮助页面,我写道:

getData=read.csv('planecrashinfo_20181121001952.csv')
attach(getData)
lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
getData$date<-as.Date(date, format='%B %d, %Y')

这有效,但使用

hist(date, "years", format= '%Y')

结果在

"错误 in hist.default(date, "years", format = "%Y"( : 'x' deve essere numeric"。

是否有我没有使用的软件包?

我建议对日期字段使用lubridate

library(readr)
library(lubridate)
library(ggplot2)
getData <- read_csv("planecrashinfo_20181121001952.csv")
getData$date <- mdy(getData$date)
hist(year(getData$date))

或者用ggplot

ggplot(data = getData , aes(x = year(date))) + 
  geom_histogram(binwidth = 1)

最新更新