[英]how to draw histogram graph in r with non-numeric time data?
我得到這樣的數據:
serialno departure_time mode
1 5:34 walk
2 9:03 bus
3 6:10 car
4 7:50 car
5 9:00 car
我想繪制直方圖並計算關於變量離開時間的頻率。 x 軸為 1,2,3,4,5,6,7,8,9,10,11,12,y 軸為頻率。
謝謝!
從departure_time
時間中提取小時數並將其用於 plot。
這里有幾種方法可以做到這一點。
library(dplyr)
library(ggplot2)
df %>%
count(hours = sub(':.*', '', departure_time)) %>%
ggplot() + aes(hours, n) + geom_col()
或者
df %>%
mutate(hours = as.numeric(sub(':.*', '', departure_time))) %>%
ggplot() + aes(hours) + geom_histogram()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.