Visualization Exercise

Data Source

This data set contains the deaths involving COVID-19, pneumonia, and influenza reported to NCHS by sex, age group, and jurisdiction. This data was obtained from the Centers for Disease Control and Prevention (CDC)’s National Center for Health Statistics (NCHS).

I decided to recreate some of my figures from last week as interactive graphs.

Load Libraries

here() starts at /Users/deannalanier/Desktop/All_Classes_UGA/2023Spr_Classes/MADA/deannalanier-MADA-portfolio
Loading required package: ggplot2

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.5.0 
✔ readr   2.1.3      ✔ forcats 0.5.2 
✔ purrr   1.0.1      
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks plotly::filter(), stats::filter()
✖ dplyr::lag()    masks stats::lag()
Loading required package: timechange


Attaching package: 'lubridate'


The following objects are masked from 'package:base':

    date, intersect, setdiff, union

Read in .rds file

data_location=here("dataanalysis-exercise","output","clean_data.rds")
processedData = readRDS(data_location)

Plot 1 interactive

data_plot = processedData %>% filter(Group == "By Year")
#Create list for each of the variable that we want to separate as differnt buttons 

COVID_19 = list(
  x=processedData$Year, 
  y=processedData$COVID_19_Deaths,
  xref='x', yref='y'
)

Pneumonia = list(
  x=processedData$Year, 
  y=processedData$Pneumonia_Deaths,
  xref='x', yref='y'
)

Influenza = list(
  x=processedData$Year, 
  y=processedData$Influenza_Deaths,
  xref='x', yref='y'
)
updatemenus = list(
  list(
    active = 0,
    type= 'buttons',
    direction = "right",
    xanchor = 'right',
    yanchor = "bottom",
    pad = list('r'= 0, 't'= 0, 'b' = 0),
    x = 0.5,
    y = 1.15,
    buttons = list(
      list(
        label = "COVID-19",
        method = "update",
        args = list(list(visible = c(TRUE, FALSE, FALSE)),
                    list(title = "Covid-19 Related Deaths",
                         annotations = list(c(), c(), COVID_19)))),
      list(
        label = "Pneumonia",
        method = "update",
        args = list(list(visible = c(FALSE, TRUE, FALSE)),
                    list(title = "Pneumonia Related Deaths ",
                         annotations = list( c(), Pneumonia, c())))),
      list(
        label = "Influenza",
        method = "update",
        args = list(list(visible = c(FALSE, FALSE, TRUE)),
                    list(title = "Influenza Related Deaths",
                         annotations = list(Influenza, c(), c())))),
      list(
        label = "All",
        method = "update",
        args = list(list(visible = c(TRUE, TRUE, TRUE)),
                    list(title = "All Respiratory Virus Related Deaths",
                         annotations = list(c(), c(), c())))))
  )
)


bar_fig = plot_ly(processedData, x = ~Year, y = ~COVID_19_Deaths, type = 'bar', name = 'Covid-19 Deaths')%>% 
  add_trace(y = ~Pneumonia_Deaths, name = 'Pneumonia Deaths')%>% 
  add_trace(y = ~Influenza_Deaths, name = 'Flu Deaths')%>% 
  layout(title = list(text='All Respiratory Virus Related Deaths 2020-2021', y = 0.95, x = 0.5, xanchor = 'left', yanchor =  'bottom'),yaxis = list(title = 'Count'), barmode = 'group',xaxis = list(title = "Year"),updatemenus=updatemenus)

bar_fig

Plot data over time and add interactive slider

Month_data_location=here("dataanalysis-exercise","data","Provisional_COVID-19_Deaths_by_Sex_and_Age.csv")
MonthData = read_csv(Month_data_location)
Rows: 115668 Columns: 16
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (8): Data As Of, Start Date, End Date, Group, State, Sex, Age Group, Foo...
dbl (8): Year, Month, COVID-19 Deaths, Total Deaths, Pneumonia Deaths, Pneum...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Clean the data

#remove special characters
names(MonthData) = gsub(" ", "_",
           gsub("-", "_",
           gsub(",", "", names(MonthData))))

MonthData = MonthData %>% 
  mutate_at(
    vars('Data_As_Of', 'Start_Date','End_Date'), 
    as_date,
    format = "%m-%d-%y"
    )%>% mutate(Year = as.character(Year)) %>% 
  filter(State == "United States",Age_Group=="All Ages", Sex == "All Sexes", Group=="By Month", Year == 2020 | Year == 2021 | Year == 2022)%>%
  select(Start_Date, End_Date, COVID_19_Deaths,Pneumonia_Deaths,Influenza_Deaths, Sex, Year, Month)

###Plot

line_fig = plot_ly(MonthData, type = 'scatter', mode = 'lines', colors = "Set1")%>%
  add_trace(x = ~Start_Date, y = ~COVID_19_Deaths, name = 'Covid-19 Deaths' )%>%
  add_trace(x = ~Start_Date,y = ~Pneumonia_Deaths, name = 'Pneumonia Deaths')%>%
  add_trace(x = ~Start_Date,y = ~Influenza_Deaths, name = 'Flu Deaths')%>%
  layout(paper_bgcolor='#D4D4D4',plot_bgcolor='#D4D4D4',showlegend = T, title='Respiratory virus related deaths 2020-2022',
         xaxis = list(rangeslider = list(visible = T),
                      rangeselector=list(
                        buttons=list(
                          list(count=1, label="1m", step="month", stepmode="backward"),
                          list(count=6, label="6m", step="month", stepmode="backward"),
                          list(count=1, label="1y", step="year", stepmode="backward"),
                          list(step="all")
                        ))))
line_fig = line_fig %>%
  layout(
         xaxis = list(zerolinecolor = '#D4D4D4',
                      zerolinewidth = 2,
                      gridcolor = '#D4D4D4',color ='000000', title = 'Date'),
         yaxis = list(zerolinecolor = '#D4D4D4',
                      zerolinewidth = 2,
                      gridcolor = '#D4D4D4', color ='000000',title = '# of Deaths'),
         plot_bgcolor='#fff', width = 800)
Warning: Specifying width/height in layout() is now deprecated.
Please specify in ggplotly() or plot_ly()
line_fig

##Comments Using Plotly in R was fairly straight forward and I didnt not have any difficulty making these plots interactive. My next goal is to figure out how to use the animation features.