library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(season)
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
## Loading required package: survival
?season::CVDdaily
data(CVDdaily)
CVDdaily = CVDdaily %>%
dplyr::select(cvd, date, tmpd, o3mean)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
CVDdaily = CVDdaily %>% mutate(date = date %>% date())
CVDdaily = CVDdaily %>% mutate(month = date %>% month())
?month
## how to get month name
CVDdaily = CVDdaily %>% mutate(month = date %>% month(label = T))
CVDdaily %>%
ggplot(aes(x=month, y=cvd))+
geom_violin()+
stat_boxplot(width=0.1, fill = "cyan4")+
ylab('Daily number of CVD deaths')+
xlab('Month')+
theme_bw()
## same but for day of week
CVDdaily %>%
ggplot(aes(x=date, y=cvd))+
geom_line()+
ylab('Daily number of CVD deaths')+
xlab('Time')+
theme_bw()
CVDdaily %>%
mutate(cvd_grow = cvd + row_number()/30) %>%
ggplot(aes(x=date, y=cvd_grow))+
geom_line()+
ylab('Daily number of CVD deaths (simulated)')+
xlab('Time')+
theme_bw()
(CVDdaily$date %>% max() - CVDdaily$date %>% min()) %>% as.duration()
## [1] "441763200s (~14 years)"
Januarys = c(date("1987-01-01") + years(1:14))
CVDdaily %>%
mutate(years = date %>% year()) -> CVDdaily
CVDdaily %>%
ggplot(aes(x=date, y=cvd))+
geom_line()+
scale_x_continuous(breaks=Januarys, labels=Januarys %>% year())+
ylab('Daily number of CVD deaths')+
xlab('Year')+
theme_bw()+
theme(panel.grid.minor = element_blank())
### draw over a year aka seasonplot
CVDdaily %>%
mutate(day_of_year = date %>% yday()) %>%
ggplot(aes(x=day_of_year, y=cvd, color = years), alpha = 0.1)+
geom_line()+
# scale_x_continuous(breaks=Januarys, labels=Januarys %>% year())+
ylab('Daily number of CVD deaths')+
xlab('Day of year')+
theme_bw()+
theme(panel.grid.minor = element_blank())
## draw for a single year with dplyr