[英]Simple plotly histogram of monthly frequencies in R
So I know that this is a super simple question, but still I could not resolve it.所以我知道这是一个超级简单的问题,但我仍然无法解决它。 I have data that looks like this with precise dates in one year:我的数据看起来像这样,一年内有精确的日期:
date
1 2008-08-08
2 2008-08-08
3 2008-04-12
4 <NA>
5 <NA>
6 2008-04-10
7 2008-07-12
8 2008-04-10
9 <NA>
10 2008-07-12
11 2008-07-14
12 2008-07-29
13 2008-07-29
14 2008-07-29
15 2008-08-06
16 <NA>
17 2008-08-24
18 2008-10-30
19 2008-10-30
20 2008-10-20
21 2008-01-28
22 <NA>
23 <NA>
I would like to plot the frequency of the data per month.我想 plot 每个月的数据频率。 So I do something like所以我做类似的事情
dat %>%
mutate(month = month(date),
month_posix = as.Date(paste0("2008-", month, "-01"))) %>%
plot_ly(., x = ~month_posix)
But I just don't know how to plot one bin for each month with the frequency of its appearance.但我只是不知道如何 plot 每个月一个 bin 与它的出现频率。 I tried a couple of things, but I'M just not really familiar with plotly.我尝试了几件事,但我对 plotly 不太熟悉。 Like if there are zero dates in January I would still like to see January with a bar of height 0. Maybe someone has a quick tip.就像一月份的日期为零一样,我仍然希望看到一月份的高度为 0。也许有人有一个快速提示。
Data in the format I have (just without NAs) can be produced with this command: dat = data.frame(date = sample(seq(as.Date('2008-01-01'), as.Date('2008-12-31'), by="day"), 16))
可以使用以下命令生成我拥有的格式的数据(只是没有 NA): dat = data.frame(date = sample(seq(as.Date('2008-01-01'), as.Date('2008-12-31'), by="day"), 16))
You can use count
to get the frequency in each month and then use plot_ly
您可以使用count
获取每个月的频率,然后使用plot_ly
library(plotly)
library(lubridate)
library(plotly)
dat %>%
count(month = month(date), name = 'count') %>%
plot_ly(x = ~month, y = ~count, type = 'bar')
data数据
set.seed(123)
dat = data.frame(date = sample(seq(as.Date('2008-01-01'),
as.Date('2008-12-31'), by="day"), 50))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.