\(~\)
I have provided you with data about mortality from all 50 states and the District of Columbia. Please access it at https://github.com/charleyferrari/CUNY_DATA608/tree/master/module3/data You are invited to gather more data from our provider, the CDC WONDER system, at https://wonder.cdc.gov/ucd-icd10.html.
This assignment must be done in R. It must be done using the ‘shiny’ package.
It is recommended you use an R package that supports interactive graphing such as plotly, or vegalite, but this is not required.
Your apps must be deployed, I won’t be accepting raw files. Luckily, you can pretty easily deploy apps with a free account at shinyapps.io
\(~\)
library(tidyverse)
library(ggplot2)
library(rsconnect)
library(plotly)
library(shiny)
<- read.csv("https://raw.githubusercontent.com/charleyferrari/CUNY_DATA_608/master/module3/data/cleaned-cdc-mortality-1999-2010-2.csv", header = TRUE) data
str(data)
## 'data.frame': 9961 obs. of 6 variables:
## $ ICD.Chapter: chr "Certain infectious and parasitic diseases" "Certain infectious and parasitic diseases" "Certain infectious and parasitic diseases" "Certain infectious and parasitic diseases" ...
## $ State : chr "AL" "AL" "AL" "AL" ...
## $ Year : int 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 ...
## $ Deaths : int 1092 1188 1211 1215 1350 1251 1303 1312 1241 1385 ...
## $ Population : int 4430141 4447100 4467634 4480089 4503491 4530729 4569805 4628981 4672840 4718206 ...
## $ Crude.Rate : num 24.6 26.7 27.1 27.1 30 27.6 28.5 28.3 26.6 29.4 ...
summary(data)
## ICD.Chapter State Year Deaths
## Length:9961 Length:9961 Min. :1999 Min. : 10
## Class :character Class :character 1st Qu.:2002 1st Qu.: 177
## Mode :character Mode :character Median :2005 Median : 667
## Mean :2005 Mean : 2929
## 3rd Qu.:2008 3rd Qu.: 2474
## Max. :2010 Max. :96511
## Population Crude.Rate
## Min. : 491780 Min. : 0.00
## 1st Qu.: 1728292 1st Qu.: 4.60
## Median : 4219239 Median : 24.00
## Mean : 5937896 Mean : 52.15
## 3rd Qu.: 6562231 3rd Qu.: 50.50
## Max. :37253956 Max. :478.40
colnames(data)
## [1] "ICD.Chapter" "State" "Year" "Deaths" "Population"
## [6] "Crude.Rate"
colSums(is.na(data))
## ICD.Chapter State Year Deaths Population Crude.Rate
## 0 0 0 0 0 0
As a researcher, you frequently compare mortality rates from particular causes across different States. You need a visualization that will let you see (for 2010 only) the crude mortality rate, across all States, from one cause (for example, Neoplasms, which are effectively cancers). Create a visualization that allows you to rank States by crude mortality for each cause of death.
<- filter(data, Year == '2010' & ICD.Chapter == 'Certain infectious and parasitic diseases')
infec_diseases <- infec_diseases %>%
infec_diseases arrange(Crude.Rate)
head(infec_diseases)
## ICD.Chapter State Year Deaths Population
## 1 Certain infectious and parasitic diseases UT 2010 274 2763885
## 2 Certain infectious and parasitic diseases ID 2010 174 1567582
## 3 Certain infectious and parasitic diseases ND 2010 83 672591
## 4 Certain infectious and parasitic diseases AK 2010 88 710231
## 5 Certain infectious and parasitic diseases MN 2010 678 5303925
## 6 Certain infectious and parasitic diseases NE 2010 245 1826341
## Crude.Rate
## 1 9.9
## 2 11.1
## 3 12.3
## 4 12.4
## 5 12.8
## 6 13.4
<- infec_diseases %>% plot_ly() %>%
fig add_trace(x = ~Crude.Rate, y = ~State, type = 'bar',
text = 'y', textposition = 'auto',
marker = list(color = 'rgb(158,202,225)',
line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
layout(title = "Certain infectious and parasitic diseases",
barmode = 'group',
xaxis = list(title = "Crude rate"),
yaxis = list(title = "States"))
fig
# Define UI for app
<- fluidPage(
ui # App title ----
titlePanel("Crude Mortality Rate"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input:
selectInput(inputId = 'y','Selected ICD.Chapter:',
choices = infec_diseases$ICD.Chapter,
selected = 'State')
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Create barchart
tabsetPanel(type = "tabs",
tabPanel("Plot", plotlyOutput('distPlot'))
)
)
)
)# Define server logic required to draw a bargraph
<- function(input, output) {
server $distPlot <- renderPlotly({fig})
output
}
# Run the application
shinyApp(ui, server)
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Often you are asked whether particular States are improving their mortality rates (per cause) faster than, or slower than, the national average. Create a visualization that lets your clients see this for themselves for one cause of death at the time. Keep in mind that the national average should be weighted by the national population.
<- data %>%
infec_diseases_2 group_by(Year, ICD.Chapter) %>%
filter(ICD.Chapter == "Certain infectious and parasitic diseases") %>%
mutate(Crude.Rate_2 = round(
sum(Deaths) / sum(Population) * 100000),3) %>%
group_by(Year, ICD.Chapter, State)
head(infec_diseases_2)
## # A tibble: 6 × 8
## # Groups: Year, ICD.Chapter, State [6]
## ICD.Chapter State Year Deaths Population Crude.Rate Crude.Rate_2 `3`
## <chr> <chr> <int> <int> <int> <dbl> <dbl> <dbl>
## 1 Certain infectiou… AL 1999 1092 4430141 24.6 21 3
## 2 Certain infectiou… AL 2000 1188 4447100 26.7 21 3
## 3 Certain infectiou… AL 2001 1211 4467634 27.1 21 3
## 4 Certain infectiou… AL 2002 1215 4480089 27.1 22 3
## 5 Certain infectiou… AL 2003 1350 4503491 30 22 3
## 6 Certain infectiou… AL 2004 1251 4530729 27.6 22 3
# This plot was not working out for some reason
# state = "CA"
# fig_2<- infec_diseases_2 %>%
# plot_ly(x=~Year, y=~Crude.Rate, type ='bar',
# #text = y, textposition = 'auto',
# marker = list(color = 'rgb(158,202,225)'),
# name = 'State') %>% # Chart State and US Crude Rates next to one another
# add_trace(x=~Year, y=~Crude.Rate_2, type='bar',
# #text = y, textposition = 'auto',
# marker = list(color = 'rgb(58,200,225)'), name = 'US') %>%
# layout(title = "Crude death rate by state [CA]",
# barmode = 'group',
# xaxis = list(title = "Year"),
# yaxis = list(title = "Crude rate"))
# fig_2
# Trying this new plot and it came out much better than plot 1
= "CA"
state <- infec_diseases_2 %>%
fig_2 as_tibble() %>%
filter(., State == state) %>%
select(., Year, Crude.Rate, Crude.Rate_2) %>%
plot_ly(x = ~Year, y = ~Crude.Rate, type='bar',
text = ~Crude.Rate, textposition = 'auto',
marker = list(color = 'rgb(158,202,225)'),
name = 'State') %>%
add_trace(x = ~Year, y = ~Crude.Rate_2, type='bar',
text = ~Crude.Rate_2, textposition = 'auto',
marker = list(color = 'rgb(58,200,225)'), name = 'CA') %>%
layout(title = "Crude death rate by state [CA]",
barmode = 'group',
xaxis = list(title = "Year"),
yaxis = list(title = "Crude rate"))
fig_2
# Define UI for app
<- fluidPage(
ui # App title ----
titlePanel("Crude Mortality Rate"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input:
selectInput(inputId = 'y','Selected ICD.Chapter:',
choices = infec_diseases_2$ICD.Chapter,
selected = 'State')
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Create barchart
tabsetPanel(type = "tabs",
tabPanel("Plot", plotlyOutput('distPlot2'))
)
)
)
)<- function(input, output) {
server $distPlot2 <- renderPlotly({fig_2})
output
}
# Run the application
shinyApp(ui, server)
\(~\)
## Preparing to deploy application...DONE
## Uploading bundle for application: 7225958...DONE
## Deploying bundle: 6376824 for application: 7225958 ...
## Waiting for task: 1235049493
## building: Parsing manifest
## building: Building image: 7470238
## building: Fetching packages
## building: Installing packages
## building: Installing files
## building: Pushing image: 7470238
## deploying: Starting instances
## rollforward: Activating new instances
## terminating: Stopping old instances
## Application successfully deployed to https://letisalba.shinyapps.io/module3/