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. 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. I tried a couple of things, but I'M just not really familiar with 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.
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))
You can use count
to get the frequency in each month and then use 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))