Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.38 KB | None | 0 0
  1. library(dplyr)
  2. library(shiny)
  3. library(ggplot2)
  4. library(readxl)
  5. master<-read_excel("~/Domaca Naloga/selected-trend-table-from-health-united-states-2011.-leading-causes-of-death-and-numbers-of-deaths-by-sex-race-and-hispanic-origin-united-states-1980-and-2009.xlsx")
  6. uni <- unique(as.factor(master$Cause_of_death))
  7.  
  8.  
  9.  
  10. # Define UI for application that draws a histogram
  11. ui <- fluidPage(
  12.  
  13.   # Application title
  14.   titlePanel("Leading Causes of Death and Numbers of Deaths "),
  15.   HTML("<p>In the table belowe we have data on the number of deaths, throughout the years starting since 1980 to 2009, for a number of differnet causes.
  16.       Including various diseases, suicide and homicide. This is a dataset hosted by the Centers for Disease Control and Prevention.
  17.       Based on this dataset I will try to find out things like: what was the leading couse of death at the begining, end and throughout this period,
  18.       have more people died at the end of the period or at the begining, are there diseases that affect a certan race/ethnic group (or Geographycal location) more thean others.</p>"),
  19.   conditionalPanel(condition = "input.show_data == true", h3("Data table")),
  20.   DT::dataTableOutput(outputId = "deathtable"),
  21.   # Sidebar with a slider input for number of bins
  22.   sidebarLayout(
  23.     sidebarPanel(
  24.       selectInput(inputId = "Disease",
  25.                   label = "Select Disease:",
  26.                   choices = uni,
  27.                   selected = "All causes",
  28.                   selectize = TRUE),
  29.      
  30.      
  31.       wellPanel(
  32.         # Show data table
  33.         checkboxInput(inputId = "show_data",
  34.                       label = "Show data table",
  35.                       value = TRUE)
  36.       ),
  37.      
  38.       HTML("<h3>Data interpretation</h3>
  39.           <p>From the dropdown menu above we can choose between different causes of death.
  40.           On the right we get a chart showing us if the the number of deaths is growing or not for different races/ethnic groups.
  41.           From the data we can conclude that while some diseases are occuring less the other that have grown in time. We can also see that there are diseases
  42.           that appear to affect more certan races/ethnic groups. That can be caused by Geographycal or Biologycal predispositions.
  43.           Overall when we select all causes we can see that the number of deaths has grown from 200 000 to 250 000.</p>")
  44.      
  45.      
  46.       ), #closing sidebarPanel
  47.     # Show a plot of the generated distribution
  48.    
  49.     mainPanel(
  50.       plotOutput(outputId = "scatterplot"),
  51.       width = 7
  52.      
  53.     )
  54.    
  55.     ), #closing sidebarLayout
  56.  
  57.   plotOutput(outputId = "barchart"),
  58.   HTML("<h3>Data interpretation</h3>
  59.        <p>From the bar char above we can see the number of deaths for groups throughout the years,
  60.       and from the chart below we can see the number of deaths by diseases.</p>"),
  61.   plotOutput(outputId = "barchart2")
  62.   )#closing fluidPage
  63.  
  64. # Define server logic required to draw a histogram
  65. server <- function(input, output) {
  66.  
  67.   output$deathtable <- DT::renderDataTable(
  68.     if(input$show_data){
  69.       DT::datatable(data = master[, 1:4],
  70.                     options = list(pageLength = 10),
  71.                     rownames = FALSE)
  72.     }
  73.   )
  74.  
  75.   output$scatterplot <- renderPlot({
  76.     #req(input$category)
  77.     req(input$Disease)
  78.     filter(master, Cause_of_death==input$Disease) %>%
  79.       ggplot(., aes(x= Year, y=Deaths, group=Group, color=Group)) +
  80.       geom_line(size=1.5) + geom_point(shape=4, size=2.5)+
  81.       scale_color_brewer(palette="Set2")+
  82.       theme_dark()
  83.   })
  84.  
  85.   output$barchart <- renderPlot({
  86.     #req(input$category)
  87.     #req(input$Disease)
  88.     ggplot(master, aes(x= Year, y= Deaths, fill=Group)) +
  89.       geom_bar(stat="identity", width = 9, colour="black", lwd=0.1) +
  90.       geom_text(aes(label= ifelse(Deaths>300000,Deaths,"")), position=position_stack(vjust=0.5), colour = "white") + coord_flip() + labs(y="", x="")
  91.   })
  92.  
  93.   output$barchart2 <- renderPlot({
  94.     #req(input$category)
  95.     #req(input$Disease)
  96.     ggplot(master, aes(x= Year, y= Deaths, fill=Cause_of_death)) +
  97.       geom_bar(stat="identity", width = 9, colour="black", lwd=0.1) +
  98.       geom_text(aes(label= ifelse(Deaths>500000,Deaths,"")), position=position_stack(vjust=0.5), colour = "white") + coord_flip() + labs(y="", x="")
  99.   })
  100. }
  101.  
  102. # Run the application
  103. shinyApp(ui = ui, server = server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement