按一天中的小时发生的直方图
Histogram for occurrences by hour-of-day
我有一个包含时间的向量。我想知道每小时的时间频率。
矢量看起来像这样
head(time_col)
[1] "2021-11-23 09:00:00 GMT" "2021-11-23 13:55:00 GMT"
[3] "2021-11-23 01:25:00 GMT" "2021-11-23 01:50:00 GMT"
[5] "2021-11-23 02:25:00 GMT" "2021-11-23 01:30:00 GMT"
hist(time_col,"hours",main = "Density of accidents in each hour" , xlab = "hour" , ylab =
"density",col="green")
我想要频率直方图
您的数据:
df <- data.frame(time_col=c("2021-11-23 09:00:00 GMT",
"2021-11-23 13:55:00 GMT",
"2021-11-23 01:25:00 GMT",
"2021-11-23 01:50:00 GMT",
"2021-11-23 02:25:00 GMT",
"2021-11-23 01:30:00 GMT") )
加载库:
library(lubridate) # for date-time manipulation
library(tidyverse) # for the handy '%>%', mutate() and summarise()
library(ggplot2) # for plotting
按小时计算出现次数
df %>% mutate(time_col = ymd_hms(time_col)) %>%
mutate(hour = hour(time_col)) %>%
group_by(hour) %>% summarise(freq = n()) -> hour_freqs
ggplot2 绘图方法:
ggplot(hour_freqs, aes(x=hour, y=freq)) +
geom_bar(fill = 'green', stat='identity')
对于基础 R,你可以这样做:
barplot(hour_freqs$hour, hour_freqs$freq)
axis(2, seq(0, 5, by=1))
我有一个包含时间的向量。我想知道每小时的时间频率。 矢量看起来像这样
head(time_col)
[1] "2021-11-23 09:00:00 GMT" "2021-11-23 13:55:00 GMT"
[3] "2021-11-23 01:25:00 GMT" "2021-11-23 01:50:00 GMT"
[5] "2021-11-23 02:25:00 GMT" "2021-11-23 01:30:00 GMT"
hist(time_col,"hours",main = "Density of accidents in each hour" , xlab = "hour" , ylab =
"density",col="green")
我想要频率直方图
您的数据:
df <- data.frame(time_col=c("2021-11-23 09:00:00 GMT",
"2021-11-23 13:55:00 GMT",
"2021-11-23 01:25:00 GMT",
"2021-11-23 01:50:00 GMT",
"2021-11-23 02:25:00 GMT",
"2021-11-23 01:30:00 GMT") )
加载库:
library(lubridate) # for date-time manipulation
library(tidyverse) # for the handy '%>%', mutate() and summarise()
library(ggplot2) # for plotting
按小时计算出现次数
df %>% mutate(time_col = ymd_hms(time_col)) %>%
mutate(hour = hour(time_col)) %>%
group_by(hour) %>% summarise(freq = n()) -> hour_freqs
ggplot2 绘图方法:
ggplot(hour_freqs, aes(x=hour, y=freq)) +
geom_bar(fill = 'green', stat='identity')
对于基础 R,你可以这样做:
barplot(hour_freqs$hour, hour_freqs$freq)
axis(2, seq(0, 5, by=1))