R Markdown

Loading Data

datamortality <- read.csv("https://raw.githubusercontent.com/charleyferrari/CUNY_DATA_608/master/module3/data/cleaned-cdc-mortality-1999-2010-2.csv")

Question 1: 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.

diseases <- filter(datamortality , 
                              Year == 2010 & ICD.Chapter == 'Certain infectious and parasitic diseases')

diseases <- diseases %>% arrange(Crude.Rate)

head(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
ggplot(diseases, aes(x=reorder(State, -Crude.Rate), y=Crude.Rate))+
  geom_col(fill='steelblue')+
  coord_flip()+
  geom_text(aes(label=Crude.Rate),
            size=3,
            hjust=-0.5,
            color="black")+
  xlab("State")+
  ylab("Rate")+
  theme(axis.text.x = element_text(angle=65, vjust=0.6))

# define UI layout for the shiny app

ui <- fluidPage(
  headerPanel('State Moratlity Rate by Cause'),
  sidebarPanel(
    selectInput('Cause', 'Cause', unique(datamortality$ICD.Chapter),
                selected='Certain infectious and parasitic diseases') #will display default cause
    
  ),
  mainPanel(
    htmlOutput(outputId = 'selection'),
    plotOutput('plot1', height="auto"),
    h6("The Crude Mortality Rate across all States")
  )
)

# define server logic for data and input

server <- shinyServer(function(input, output, session) {
  selectedData <- reactive({
   datamortality %>% filter(ICD.Chapter == input$Cause & Year == 2010 )
  })
  
  output$selection <- renderText({
    paste('<b> Crude rate for: </b>', input$Cause)
  })
  
  output$plot1 <- renderPlot({
    
    ggplot(selectedData(), aes(x=reorder(State, -Crude.Rate), y=Crude.Rate)) +
      geom_col(fill = "#ffa600") +
      coord_flip() +
      geom_text(aes(label=Crude.Rate),
                size=3,
                hjust=-0.2,
                color="#003f5c") +
      xlab("State") +
      ylab("Crude Rate") +
      theme(panel.background = element_blank())
  }, height = function() {
    session$clientData$output_plot1_width}
  )
})

shinyApp(ui = ui, server = 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.

Shiny applications not supported in static R Markdown documents

Question 2: 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.

nationdata <- datamortality %>%
    select(ICD.Chapter, Year, State, Deaths, Population, Crude.Rate) %>%
    mutate(Crude.Rate = round((sum(Deaths) / sum(Population)) * 10^5, 1)) %>%
    mutate(State = "Statedata") %>%
    select(ICD.Chapter, Year, Crude.Rate, State) %>%
    group_by(ICD.Chapter, Year)

nationdata$State <- as.character(nationdata$State)



statemor <- datamortality %>%
    select(ICD.Chapter, Year, Crude.Rate, State)

head(statemor)
##                                 ICD.Chapter Year Crude.Rate State
## 1 Certain infectious and parasitic diseases 1999       24.6    AL
## 2 Certain infectious and parasitic diseases 2000       26.7    AL
## 3 Certain infectious and parasitic diseases 2001       27.1    AL
## 4 Certain infectious and parasitic diseases 2002       27.1    AL
## 5 Certain infectious and parasitic diseases 2003       30.0    AL
## 6 Certain infectious and parasitic diseases 2004       27.6    AL
# Creating New DataFrame

df1 <- union_all(statemor, nationdata)

#creating layout with ui - user inputs State and Cause
ui <- fluidPage(
    headerPanel('State Mortality Rates'),
    sidebarPanel(
        selectInput('State', 'State', unique(df1$State), selected='DE'),
        selectInput('ICD.Chapter', 'Cause', unique(df1$ICD.Chapter), selected='Certain infectious and parasitic diseases')
    ),
    mainPanel(
        plotlyOutput('plot1')
    )
)


server <- function(input, output, session) {
    
    nationalData <- reactive({
        nationdata %>%
            filter(ICD.Chapter == input$ICD.Chapter)
    })
    
    statedata <- reactive({
         df1 %>%
            filter(State == input$State, ICD.Chapter == input$ICD.Chapter)
    })
    
    combined <- reactive({
        merge(x = nationalData(), y = statedata(), all = TRUE)
    })
    
    output$plot1 <- renderPlotly({
        
        df1 %>%
            filter(State == input$State, ICD.Chapter == input$ICD.Chapter)
        
        plot_ly(combined(), x = ~Year, y = ~Crude.Rate, color = ~State, type='scatter',
                mode = 'lines')
    })
    
}

shinyApp(ui = ui, server = server)

Shiny applications not supported in static R Markdown documents