日期的频率计数

时间:2019-04-04 16:03:48

标签: r date ggplot2 histogram frequency

我有一个带有日期列的数据框。我正在尝试获取每个日期的频率计数。我以为直方图可以很好地可视化数据,但是也许有更好的方法?我能够创建数据的直方图,但这并不是我想要的。我希望在x轴上获得每个单独的日期,在y轴上获得频率计数。

我已经在R中进行了一些编程,但是在R中没有进行太多的可视化。任何帮助将不胜感激。

RawDates<- c("11/8/2017","12/6/2017","10/6/2017","12/6/2017","1/24/2018","9/5/2017","1/24/2018","2/21/2018","10/12/2017","1/22/2018","5/2/2018","1/24/2018","10/12/2017","1/22/2018","2/21/2018","5/2/2018","3/12/2018","5/3/2018","11/7/2017","12/5/2017","9/8/2017","10/6/2017","10/5/2017","11/3/2017","12/6/2017","2/21/2018","11/2/2017","12/5/2017","5/2/2018","1/24/2018","9/6/2017","11/2/2017","2/21/2018","5/2/2018","1/24/2018","11/8/2017","3/12/2018","5/3/2018","1/24/2018")
FormattedDates <- as.Date(RawDates, format = "%m/%d/%Y")

df <- data.frame(FormattedDates)

##This is whatI have already tried
hist(df$FormattedDates, "days", format = "%m/%d/%Y")

1 个答案:

答案 0 :(得分:2)

这里有一个简单的ggplot2解决方案:

library(ggplot2)
library(scales)

ggplot(df) +
 geom_histogram(aes(x = FormattedDates)) +
 scale_x_date(labels = date_format("%m %d %Y"), date_breaks = "30 days") +
 theme(legend.position = "bottom",
       axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here