Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 5.24 KB | None | 0 0
  1. #
  2. library(dplyr)
  3. library(shiny)
  4. library(ggplot2)
  5. master<-read.csv(url("http://www.studenti.famnit.upr.si/~89161011/masterBig.csv"))
  6. master<-masterBig
  7.  
  8. master2<-master %>% group_by(country,year,sex) %>% summarise(GDP = mean(GDP),suicides_no=sum(suicides_no),population=sum(population))
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. # Define UI for application that draws a histogram
  16. ui <- fluidPage(
  17.    
  18.    # Application title
  19.    titlePanel("Suicid rate compared to gender, GDP, age and population"),
  20.    HTML("<p>In this project I am trying to investigate suicidal rates in the world and whether things like gender, age,
  21.        GDP of certain country or population size plays role in predicting future rates. By comparing results of each country we can see which one
  22.        was the most succesful in tackling this issue</p>"),
  23.    
  24.    # Sidebar with a slider input for number of bins
  25.    sidebarLayout(
  26.       sidebarPanel(
  27.         selectInput(inputId = "country",
  28.                     label = "Select country:",
  29.                     choices = master2$country,
  30.                     selected = "Slovenia",
  31.                     multiple = TRUE,
  32.                     selectize = TRUE),
  33.        
  34.         radioButtons("dist", "Choose criteria:",
  35.                      c("Suicid rate according to gender" = "sexbutton",
  36.                        "Suicid rate according to GDP" = "GDPbutton",
  37.                        "Suicid rate according to age" = "agebutton",
  38.                        "Suicid-population growth relation" = "populationbutton")),
  39.        
  40.        
  41.         wellPanel(
  42.           # Show data table
  43.           checkboxInput(inputId = "show_data",
  44.                         label = "Show data table",
  45.                         value = TRUE)
  46.         ),
  47.        
  48.         HTML("<h3>Data interpretation</h3>
  49.             <p>As we switch between radio buttons in order to see how GDP,gender,age and population affect suicidal rates accros different countries.
  50.             By doing data modeling and visualization we get a chance to draw a conclusion that gender plays huge role. Males commit suicide more often in
  51.             all observations. Another conlcusion we could make is that suicidal rate is reciprocal to GDP growth in most observations. Most observations showed
  52.             that population grows despite high level of suicids. People of age 35-54 are more likely commit suicide than any other age group. Kids aged from 5 to 14
  53.             are not at high risk of self-harm according to our plot.Interesting thing is that in Slovenia critical age group is one from 55-74 and fact that
  54.             35-54 population is decreasing drastically which is amazing considering that this might be the only case in the whole world.</p><h4>Elmir SUT</h4>")
  55.        
  56.      
  57.           ), #closing sidebarPanel
  58.       # Show a plot of the generated distribution
  59.       mainPanel(
  60.         plotOutput(outputId = "scatterplot"),
  61.         conditionalPanel(condition = "input.show_data == true", h3("Data table")),     # Third level header: Data table
  62.         DT::dataTableOutput(outputId = "countrystable"),
  63.        
  64.         width = 7
  65.       )
  66.       ) #closing sidebarLayout
  67.   )#closing fluidPage
  68.  
  69. # Define server logic required to draw a histogram
  70. server <- function(input, output) {
  71.    
  72.    output$scatterplot <- renderPlot({
  73.      req(input$country)
  74.      if(input$dist=="sexbutton"){
  75.        master2$year<-as.factor(master2$year)
  76.      filter(master2,country==input$country) %>%
  77.        ggplot(., aes(x=year, y=suicides_no, group=sex, color=sex)) +
  78.          geom_line() + geom_point()+
  79.          scale_color_brewer(palette="Dark2")+
  80.          theme_minimal()
  81.      
  82.      }else if (input$dist=="GDPbutton"){
  83.        filter(master2,country==input$country)%>%group_by(., country,year)%>%summarize(GDP=mean(GDP),suicides_no=sum(suicides_no))%>%mutate(gdpsuicid=min(suicides_no)/min(GDP)*GDP)%>%
  84.        ggplot(., aes(x=year,y=(max(suicides_no)))) + geom_line(aes(y=suicides_no, color="suicides")) +
  85.           geom_line(aes(y=gdpsuicid, color="gdp_ratio")) +
  86.          
  87.          theme_minimal()
  88.      }else if(input$dist=="agebutton"){
  89.        master2$year<-as.factor(master2$year)
  90.        filter(master,country==input$country) %>% group_by(.,age,year)%>%summarize(suicides_no=sum(suicides_no))%>%
  91.          ggplot(., aes(x=year, y=suicides_no, group=age, color=age)) +
  92.          geom_line(size=1.5) + geom_point(size=1.5)+
  93.          scale_color_brewer(palette="Set2")+
  94.          theme_dark()
  95.      } else{
  96.        filter(master,country==input$country) %>% group_by(.,year)%>%summarize(.,suicides_no=sum(suicides_no),population=mean(population))%>%
  97.          ggplot(., aes(x=year,y=min(population/500))) +
  98.          geom_line(aes(y=suicides_no),col="red", size=1.1,stat="identity")+geom_line(aes(y=(population/500),col="yellow", size=1.1,stat="identity")) +
  99.          scale_color_brewer(palette="Set2")+     scale_color_discrete(name = "Population/Suicid", labels = c("Population", "Suicid rate"))+
  100.          theme_dark()
  101.      }
  102.      
  103.      
  104.    })
  105.    
  106.    output$countrystable <- DT::renderDataTable(
  107.      if(input$show_data){
  108.        DT::datatable(data = master2[, 1:6],
  109.                      options = list(pageLength = 10),
  110.                      rownames = FALSE)
  111.      }
  112.    )
  113. }
  114.  
  115. # Run the application
  116. shinyApp(ui = ui, server = server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement