Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Load Midwest
- data("midwest", package = "ggplot2")
- midwest <- data.frame(midwest)
- # Select data of interest for analysis
- table <- select(midwest, county, state, poptotal, popdensity, percollege, percprof,
- percpovertyknown, inmetro)
- table <- table %>%
- mutate(inmetro = gsub("0", "Not Available", inmetro),
- inmetro = gsub("1", "Available", inmetro))
- # Prep data for widget
- min_density <- min(table$popdensity)
- max_density <- max(table$popdensity)
- min_percprof <- min(table$percprof)
- max_percprof <- max(table$percprof)
- # Create ui page
- ui_page <- navbarPage(
- tabPanel(
- "Urbanization",
- titlePanel("Urbanization vs. Poverty Rate"),
- sidebarLayout(
- sidebarPanel(
- # Slider (Widget1)
- h4("Select Population Density Range of Interest"),
- sliderInput("popdensity",
- label = h4("Population Density"),
- min = min_density,
- max = max_density,
- value = c(min_density, max_density)
- ),
- # Check Box (Widget2)
- h4("Select State(s) of Interest"),
- checkboxGroupInput("States",
- label = h3("Select the State(s)"),
- choices = list("IL" = "IL", "IN" = "IN", "MI" = "MI", "OH" = "OH", "WI" = "WI"),
- selected = NULL),
- hr(),
- fluidRow
- ),
- mainPanel(
- plotOutput("bar_chart")
- )
- )
- ),
- tabPanel(
- "Profession",
- titlePanel("Professional Occupation vs. Poverty Rate"),
- sidebarLayout(
- sidebarPanel(
- h4("Select Percent Population with Professional Occupation Range of Interest"),
- sliderInput("percprof",
- label = h4("Percent Population with Professional Occupation"),
- min = min_percprof,
- max = max_percprof,
- value = c(min_percprof, max_percprof)
- ),
- titlePanel("States"),
- h4("Input your", strong("State"), ("of interest")),
- h6(em("Please enter the abbreviation in all caps.")),
- textInput("state", label = "State of Interest")
- #textOutput("state_name")
- ),
- mainPanel(
- plotOutput("scatter_plot")
- )
- )
- )
- )
- # Slider of Population Density and Checkbox (PAGE 1)
- filter1 <- reactive({
- filtered_data <- filter(table, popdensity >= input$popdensity[[1]], poptotal <= input$popdensity[[2]])
- if (input$States != "") {
- filtered_table <- table %>%
- filter(popdensity >= input$popdensity[[1]], popdensity <= input$popdensity[[2]]) %>%
- filter(state %in% input$States) %>%
- group_by(inmetro) %>%
- summarize(avg_poverty = mean(percpovertyknown))
- }
- return(filtered_data)
- })
- # Bar Chart Output
- output$bar_chart <- renderPlot({
- bar_chart <- ggplot(data = filter1(), aes(x = inmetro, y = avg_poverty)) +
- geom_bar(stat = "identity", fill = "steelblue") +
- theme_minimal() +
- labs(title = "Urbanization vs. Poverty Rate",
- x = "Metro Availability",
- y = "Average Poverty Rate")
- bar_chart
- })
- # Slider of Percent Profession and Text Input (PAGE 2)
- filter2 <- reactive({
- filtered <- filter(table, percprof >= input$percprof[[1]], percprof <= input$percprof[[2]])
- if (input$state != "") {
- filtered <- table %>%
- filter(percprof >= input$percprof[[1]], percprof <= input$percprof[[2]]) %>%
- filter(state == input$state)
- }
- return(filtered)
- })
- # Scatter Plot
- output$scatter_plot <- renderPlot({
- scatter_plot <- ggplot(data = filter2(), mapping = aes(x = percprof, y = percpovertyknown)) +
- geom_point(aes(col = state), size = 3) +
- geom_smooth(method = "lm", col = "firebrick", size = 2) +
- coord_cartesian(xlim = c(0, max_percprof), ylim = c(0, 100)) +
- labs(title = "Percent with Professional Occupation vs. Poverty Rate",
- x = "Percent of Population with Professional Occupation",
- y = "Poverty Rate")
- scatter_plot
- })
- }```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement