Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. # Load Midwest
  2. data("midwest", package = "ggplot2")
  3. midwest <- data.frame(midwest)
  4.  
  5. # Select data of interest for analysis
  6. table <- select(midwest, county, state, poptotal, popdensity, percollege, percprof,
  7. percpovertyknown, inmetro)
  8. table <- table %>%
  9. mutate(inmetro = gsub("0", "Not Available", inmetro),
  10. inmetro = gsub("1", "Available", inmetro))
  11.  
  12. # Prep data for widget
  13. min_density <- min(table$popdensity)
  14. max_density <- max(table$popdensity)
  15. min_percprof <- min(table$percprof)
  16. max_percprof <- max(table$percprof)
  17.  
  18. # Create ui page
  19. ui_page <- navbarPage(
  20. tabPanel(
  21. "Urbanization",
  22. titlePanel("Urbanization vs. Poverty Rate"),
  23. sidebarLayout(
  24. sidebarPanel(
  25. # Slider (Widget1)
  26. h4("Select Population Density Range of Interest"),
  27. sliderInput("popdensity",
  28. label = h4("Population Density"),
  29. min = min_density,
  30. max = max_density,
  31. value = c(min_density, max_density)
  32. ),
  33.  
  34. # Check Box (Widget2)
  35. h4("Select State(s) of Interest"),
  36. checkboxGroupInput("States",
  37. label = h3("Select the State(s)"),
  38. choices = list("IL" = "IL", "IN" = "IN", "MI" = "MI", "OH" = "OH", "WI" = "WI"),
  39. selected = NULL),
  40. hr(),
  41. fluidRow
  42. ),
  43. mainPanel(
  44. plotOutput("bar_chart")
  45. )
  46. )
  47. ),
  48.  
  49. tabPanel(
  50. "Profession",
  51. titlePanel("Professional Occupation vs. Poverty Rate"),
  52. sidebarLayout(
  53. sidebarPanel(
  54. h4("Select Percent Population with Professional Occupation Range of Interest"),
  55. sliderInput("percprof",
  56. label = h4("Percent Population with Professional Occupation"),
  57. min = min_percprof,
  58. max = max_percprof,
  59. value = c(min_percprof, max_percprof)
  60. ),
  61. titlePanel("States"),
  62. h4("Input your", strong("State"), ("of interest")),
  63. h6(em("Please enter the abbreviation in all caps.")),
  64. textInput("state", label = "State of Interest")
  65. #textOutput("state_name")
  66. ),
  67. mainPanel(
  68. plotOutput("scatter_plot")
  69. )
  70. )
  71. )
  72. )
  73.  
  74. # Slider of Population Density and Checkbox (PAGE 1)
  75. filter1 <- reactive({
  76. filtered_data <- filter(table, popdensity >= input$popdensity[[1]], poptotal <= input$popdensity[[2]])
  77. if (input$States != "") {
  78. filtered_table <- table %>%
  79. filter(popdensity >= input$popdensity[[1]], popdensity <= input$popdensity[[2]]) %>%
  80. filter(state %in% input$States) %>%
  81. group_by(inmetro) %>%
  82. summarize(avg_poverty = mean(percpovertyknown))
  83. }
  84. return(filtered_data)
  85. })
  86.  
  87. # Bar Chart Output
  88. output$bar_chart <- renderPlot({
  89. bar_chart <- ggplot(data = filter1(), aes(x = inmetro, y = avg_poverty)) +
  90. geom_bar(stat = "identity", fill = "steelblue") +
  91. theme_minimal() +
  92. labs(title = "Urbanization vs. Poverty Rate",
  93. x = "Metro Availability",
  94. y = "Average Poverty Rate")
  95. bar_chart
  96. })
  97.  
  98. # Slider of Percent Profession and Text Input (PAGE 2)
  99. filter2 <- reactive({
  100. filtered <- filter(table, percprof >= input$percprof[[1]], percprof <= input$percprof[[2]])
  101. if (input$state != "") {
  102. filtered <- table %>%
  103. filter(percprof >= input$percprof[[1]], percprof <= input$percprof[[2]]) %>%
  104. filter(state == input$state)
  105. }
  106. return(filtered)
  107. })
  108.  
  109. # Scatter Plot
  110. output$scatter_plot <- renderPlot({
  111. scatter_plot <- ggplot(data = filter2(), mapping = aes(x = percprof, y = percpovertyknown)) +
  112. geom_point(aes(col = state), size = 3) +
  113. geom_smooth(method = "lm", col = "firebrick", size = 2) +
  114. coord_cartesian(xlim = c(0, max_percprof), ylim = c(0, 100)) +
  115. labs(title = "Percent with Professional Occupation vs. Poverty Rate",
  116. x = "Percent of Population with Professional Occupation",
  117. y = "Poverty Rate")
  118. scatter_plot
  119. })
  120. }```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement