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
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 charactersnames(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.