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.
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.
## Importing the data
df <- read.csv("https://raw.githubusercontent.com/johnm1990/DATA608/main/cleaned-cdc-mortality-1999-2010-2.csv", header= TRUE)
## Exploring the data
head(df)
## ICD.Chapter State Year Deaths Population
## 1 Certain infectious and parasitic diseases AL 1999 1092 4430141
## 2 Certain infectious and parasitic diseases AL 2000 1188 4447100
## 3 Certain infectious and parasitic diseases AL 2001 1211 4467634
## 4 Certain infectious and parasitic diseases AL 2002 1215 4480089
## 5 Certain infectious and parasitic diseases AL 2003 1350 4503491
## 6 Certain infectious and parasitic diseases AL 2004 1251 4530729
## Crude.Rate
## 1 24.6
## 2 26.7
## 3 27.1
## 4 27.1
## 5 30.0
## 6 27.6
colnames(df)
## [1] "ICD.Chapter" "State" "Year" "Deaths" "Population"
## [6] "Crude.Rate"
To make shiny app, build two objects 1- build UI, this is the layout of how we want app to look. For example, plots in a certain place or certain buttons or select inputs. We format all these in our UI. The UI is completely static for most part. We are literally setting up a format. We put place holders and so on in this section. This is generally what the app is going to look like
2- we define our server. Anything that involves dynamic, operations, interactive we are going to define the logic for this in our server.
## Preparing the data
nat_pop <- df %>%
group_by(Year, ICD.Chapter) %>%
summarise(Population = sum(Population),
Deaths = sum(Deaths)) %>%
count(Year, Population) %>%
filter(n >1) %>% ungroup() %>%
select(-n)
## `summarise()` has grouped output by 'Year'. You can override using the `.groups` argument.
nat_deaths <- df %>%
group_by(Year, ICD.Chapter) %>%
summarise(Deaths = sum(Deaths)) %>%
ungroup() %>%
left_join(nat_pop) %>%
mutate(Crude.Rate = Deaths/Population*100000,
State = 'National') %>%
select(names(df))
## `summarise()` has grouped output by 'Year'. You can override using the `.groups` argument.
## Joining, by = "Year"
df_full <- rbind(df, nat_deaths)
# We first start off with a fluid page for UI. Our user interface will start off being put on a "blank page". Then we will define the different elements for our page. On one side(sidebarlayout) we will have our bar(with buttons) which will be used to modify the app, then other side will have outputs.Next we can define sidebar panel. These of course are all functions.
# now our sidebar panel is going to have a 'select input'. this will provide us with a drop down menu. this is a button that we can put in to filter, in this case the cause of death. We are going to take that 'cause of death' and we are going to use the value that we get there for both of the graphs. we specify first and id. our id's are what we use to refer to this input on the server side. we usually name it something relating to what is it about. for example in this case 'select_data'. our id does not show up on our visualization page, it's only visible to us 'server side'. second argument will be what we Are displaying, also know as the label. in this case 'cause of death'. this will be the message that displays on this input
# next we will specify the choices that are available. ideally, we want to choose between the different causes of death options. We call the 'unique' functions on our ICD.Chapter. This shows us 19 different causes of death. We will be able to select among those. This is our sidebar panel. Then, the other component of the sidebarlayour is the 'mainpanel', which will serve for our graphs and visualizations'. we have to make a our server object as well in order to make everything run. our 'sever' objects contains both input and output. We place all the server components within our curly brackets. it is necessary to define these things in order to have initial view of our app.
# we are going to filter by 'year' equal to 2010 and classify by 'neoplasms' cause of death. bar chart wouldn't be ideal in this scenario since we have around 50 data points. using ggplot 'aes', we will reorder by define = state / desc Crude.Rate since we are asked for crude mortality rate. we call a bar plot using geom_col().
# in our main panel we make a place holder 'plotOutput'. In our server function we can call our 'plotoutputs'. We want cause of death to be dynamic via our selectinput. We do this by setting the input for ICD.Chapter to select_death. Now we have an interactive working shiny app. we can modify 'shrunk' appearance by using plotoutput and setting our desired width/height. We could define and reference our plot1 via 'output' we render the content to our placeholder.
# next we plot trend for cause of death for a specific state. we add another select input which we utilize 'select_state'. we calculate the national rate, to do this we take our 'df' object and utilize 'groupby' which will allow us to perform operations across groups. We would group by year & ICD.Chapter in this case and then sum by population. This will give us the total of population for all the observations. The population should be the same across but number of deaths will change. Next, we will use the summarise function where we will create our new column/which will provide which statistics we desire to receive.
# next for our plot2 we take our 'df_full' and filter ICD.chapter to select_death. We utilize in the '%in%' operator to filter by a vector. For example we will keep when state is 'national' and when state is equal to 'input$select_state. Next our ggplot where we will plot x= year, y = crude.rate, color = state, giving us two lines which we can compare. We utilize geom_line.
# it is necessary to run 'shinyAPP' function with two arguments 'ui' and 'server' in order to run app. this way we will witness how visually our app is looking.
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput('select_death', 'Select a cause of death',
choices = unique(df$ICD.Chapter)),
selectInput('select_state', 'Select a state',
choices = unique(df$State))
),
mainPanel(
plotOutput('plot1', width = '85%', height = '500px'),
plotOutput('plot2', width = '85%', height = '500px')
# ggiraphOutput('plot1'),
# ggiraphOutput('plot2')
)
)
)
server <- function(input, output) {
output$plot1 <- renderPlot({
# ggiraph1 <-
df %>%
filter(Year == 2010,
ICD.Chapter == input$select_death) %>%
ggplot(aes(y = reorder(State, Crude.Rate), x = Crude.Rate,
# tooltip = Crude.Rate
)) +
geom_col(fill = 'skyblue') +
theme_minimal() +
labs(y = 'State')
# ggiraph(code = print({ggiraph1}))
})
output$plot2 <- renderPlot({
df_full %>%
filter(ICD.Chapter == input$select_death,
State %in% c('National', input$select_state)) %>%
ggplot(aes(x = Year, y = Crude.Rate, color = State,
# tooltip = Crude.Rate
)) +
geom_line() +
# geom_point_interactive(alpha = .01) +
labs(y = 'Death rate per 100k') +
theme_minimal()
})
}
shinyApp(ui, server)