Data is from National Center for Health Statistics (NCHS) Mortality Surveillance available in CSV format at https://www.cdc.gov/flu/weekly/. (URL https://www.cdc.gov/flu/weekly/weeklyarchives2020-2021/data/NCHSData??csv
where ??
is a week number.)
There are 7 variables in CSV file: year; week; pip (Percent of Deaths Due to Pneumonia and Influenza (P&I)); pic (Percent of Deaths Due to Pneumonia, Influenza or COVID-19 (PIC)); expected (Expected); threshold (Threshold); total (All Deaths); pneumonia (Pneumonia Deaths); influenza (Influenza Deaths), covid19 (COVID-19 Deaths), PIC (Pneumonia, Influenza or COVID-19 Deaths (PIC))
The R code for reading CSV file is standard:
d <- read.csv("us.pneumonia.csv", sep = ';', header=T, na.string="NA")
# wdate column is a string "yyyy/ww"
d$wdate <- sprintf ("%.4i/%.02i", d$year, d$week)
first.obs <- first(d$wdate)
last.obs <- last(d$wdate)
This first week is 2013/40 and the last is 2020/50.
Using year
and week
we compute date as defined as first day of each week: (%W
is week number of the year):
## Note if insufficient arguments provided to strptime, current date
## will be returned (ie %Y/%W is insufficent)
d$date <- strptime(sprintf("%s-%s-01", d$year, d$week), format="%Y-%W-%w")
Weekly number of deaths due to Influenza:
Weekly number of deaths due to Pneumonia, Influenza or COVID:
The same chart as above, but this time dot-plot with trend and weeks of high level flu highlighted. We define flu season as first 12 weeks of each year. To highlight these periods we define the following dataframe:
bars <- data.frame(
x1=c("2021-01-01", "2020-01-01", "2019-01-01", "2018-01-01", "2017-01-01", "2016-01-01", "2015-01-01", "2014-01-01"),
x2=c("2021-03-01", "2020-03-01", "2019-03-01", "2018-03-01", "2017-03-01", "2016-03-01", "2015-03-01", "2014-03-01"),
y1=c(0, 0, 0, 0, 0, 0, 0, 0),
y2=c(Inf,Inf, Inf, Inf, Inf, Inf, Inf, Inf)
)
Dots in flu season are drown with different color. To achieve this we define column fs
as follows:
d <- d %>% mutate( fs=case_when(week < 12 ~ "1", TRUE ~ "0"))
Next we add aes(color=as.factor(fs))
(consult the code in source Rmd file) and set_color_manual
to add legend. The legend is put under title with the declaration theme_update(legend.title=element_text(family="sans", size=8), legend.position="top" )
.
Crucial is inherit.aes = FALSE
in geom_rect
otherwise R complains about errors.
In case of errors noticed or with suggestions for improvement please contact tprzechlewski[a_t]gmail.com