Date: 10/9/2022
Load the require packages
library(shiny)
library(tidyr)
library(dplyr)
library(ggplot2)
Load Data
mortality <- read.csv("https://raw.githubusercontent.com/charleyferrari/CUNY_DATA_608/master/module3/data/cleaned-cdc-mortality-1999-2010-2.csv", header=TRUE, sep=",")
Data view
head(mortality)
## 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
Rank states by crude mortality for each cause of death
mortality_2010 <- mortality %>%
filter(Year==2010) %>%
select(State, Year, ICD.Chapter, Crude.Rate) %>%
arrange(ICD.Chapter)
mortality_2010[is.na(mortality_2010)] <- "District of Columbia"
Check whether states are improving their mortality rates compare to national average
mortality_avg <- mortality %>%
group_by(Year, ICD.Chapter) %>%
mutate(national_crude_rate = 10^5*(sum(Deaths)/sum(Population))) %>%
select(Year, State, ICD.Chapter, national_crude_rate, Crude.Rate) %>%
mutate(State_Name = state.name[match(State, state.abb)]) %>%
arrange(ICD.Chapter)
mortality_avg[is.na(mortality_avg)] <- "District of Columbia"
mortality_avg <- mortality_avg %>% gather("National_State", 'value', national_crude_rate, Crude.Rate)
Shiny App
server <- function(input, output) {
output$barplot1 <- renderPlot({
ggplot(mortality_2010[mortality_2010$ICD.Chapter==input$var1,],
aes(x=reorder(State, -Crude.Rate), y=Crude.Rate)) +
labs(x="", y = "Crude Death Rate per 100,000 Persons") +
geom_bar(stat='identity', fill="violet", border = 'white') +
scale_y_continuous(breaks = scales::pretty_breaks(n=10)) +
theme(
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x = element_text(angle=45, face="bold", size=11, vjust=1, hjust=1),
axis.text.y = element_text(face="bold", size=11),
axis.title.y = element_text(margin = margin(t=0, r=20, b=0, l=0), size=12))},
width='auto', height='auto')
output$colplot2 <- renderPlot({
ggplot(mortality_avg[mortality_avg$State_Name==input$var2 & mortality_avg$ICD.Chapter==input$var3,],
aes(x=Year, y=value, fill=National_State)) +
labs(x="", y="Crude Death Rate per 100,000 Persons") +
geom_col(position=position_dodge()) +
scale_fill_discrete(labels = c("State", "National Ave")) +
scale_x_continuous(breaks = seq(1999,2010,1)) +
scale_y_continuous(breaks = scales::pretty_breaks(n=10)) +
theme(
panel.background = element_blank(),
legend.title = element_blank(),
legend.position = "bottom",
legend.text=element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x = element_text(face="bold", size=12),
axis.text.y = element_text(face="bold", size=12),
axis.title.y = element_text(margin=margin(t=0, r=20, b=0, l=0), size=12))},
width='auto', height='auto')
}