Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- library(dplyr)
- library(shiny)
- library(ggplot2)
- master<-read.csv(url("http://www.studenti.famnit.upr.si/~89161011/masterBig.csv"))
- master<-masterBig
- master2<-master %>% group_by(country,year,sex) %>% summarise(GDP = mean(GDP),suicides_no=sum(suicides_no),population=sum(population))
- # Define UI for application that draws a histogram
- ui <- fluidPage(
- # Application title
- titlePanel("Suicid rate compared to gender, GDP, age and population"),
- HTML("<p>In this project I am trying to investigate suicidal rates in the world and whether things like gender, age,
- GDP of certain country or population size plays role in predicting future rates. By comparing results of each country we can see which one
- was the most succesful in tackling this issue</p>"),
- # Sidebar with a slider input for number of bins
- sidebarLayout(
- sidebarPanel(
- selectInput(inputId = "country",
- label = "Select country:",
- choices = master2$country,
- selected = "Slovenia",
- multiple = TRUE,
- selectize = TRUE),
- radioButtons("dist", "Choose criteria:",
- c("Suicid rate according to gender" = "sexbutton",
- "Suicid rate according to GDP" = "GDPbutton",
- "Suicid rate according to age" = "agebutton",
- "Suicid-population growth relation" = "populationbutton")),
- wellPanel(
- # Show data table
- checkboxInput(inputId = "show_data",
- label = "Show data table",
- value = TRUE)
- ),
- HTML("<h3>Data interpretation</h3>
- <p>As we switch between radio buttons in order to see how GDP,gender,age and population affect suicidal rates accros different countries.
- 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
- all observations. Another conlcusion we could make is that suicidal rate is reciprocal to GDP growth in most observations. Most observations showed
- 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
- 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
- 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>")
- ), #closing sidebarPanel
- # Show a plot of the generated distribution
- mainPanel(
- plotOutput(outputId = "scatterplot"),
- conditionalPanel(condition = "input.show_data == true", h3("Data table")), # Third level header: Data table
- DT::dataTableOutput(outputId = "countrystable"),
- width = 7
- )
- ) #closing sidebarLayout
- )#closing fluidPage
- # Define server logic required to draw a histogram
- server <- function(input, output) {
- output$scatterplot <- renderPlot({
- req(input$country)
- if(input$dist=="sexbutton"){
- master2$year<-as.factor(master2$year)
- filter(master2,country==input$country) %>%
- ggplot(., aes(x=year, y=suicides_no, group=sex, color=sex)) +
- geom_line() + geom_point()+
- scale_color_brewer(palette="Dark2")+
- theme_minimal()
- }else if (input$dist=="GDPbutton"){
- 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)%>%
- ggplot(., aes(x=year,y=(max(suicides_no)))) + geom_line(aes(y=suicides_no, color="suicides")) +
- geom_line(aes(y=gdpsuicid, color="gdp_ratio")) +
- theme_minimal()
- }else if(input$dist=="agebutton"){
- master2$year<-as.factor(master2$year)
- filter(master,country==input$country) %>% group_by(.,age,year)%>%summarize(suicides_no=sum(suicides_no))%>%
- ggplot(., aes(x=year, y=suicides_no, group=age, color=age)) +
- geom_line(size=1.5) + geom_point(size=1.5)+
- scale_color_brewer(palette="Set2")+
- theme_dark()
- } else{
- filter(master,country==input$country) %>% group_by(.,year)%>%summarize(.,suicides_no=sum(suicides_no),population=mean(population))%>%
- ggplot(., aes(x=year,y=min(population/500))) +
- 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")) +
- scale_color_brewer(palette="Set2")+ scale_color_discrete(name = "Population/Suicid", labels = c("Population", "Suicid rate"))+
- theme_dark()
- }
- })
- output$countrystable <- DT::renderDataTable(
- if(input$show_data){
- DT::datatable(data = master2[, 1:6],
- options = list(pageLength = 10),
- rownames = FALSE)
- }
- )
- }
- # Run the application
- shinyApp(ui = ui, server = server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement