資料整理

library(readr)
dta <- read.csv("calls_nyc.csv")
dta$dtah <- 0:23 #創立0~23小時的變項
knitr::kable(head(dta, 5))
Hour Calls dtah
0.5 1080 0
1.5 910 1
2.5 770 2
3.5 780 3
4.5 380 4
dts <- ts(as.numeric(dta[,2]), frequency=24)
#將Calls分為24份

class(dts)
## [1] "ts"

繪圖

library(ggplot2)
dtacall <- data.frame(Hour=factor(dta[,3]),
                      Calls=dta[,2])#轉換資料框

ggplot(dtacall, aes(Hour, Calls)) +
  geom_bar(width=1, 
          stat="identity", 
          fill="cyan", 
          col="gray", 
          alpha=0.3) + #繪製bar
  geom_abline(intercept=mean(dtacall$Calls), 
             slope=0, 
             col="red") + #平均線
    coord_polar(theta="x", 
               start=-pi/12) + #從x軸的視角,設定旋轉的角度
 theme_classic() #主題