Instructions

Create a scatter plot interactive application. In this application, use the census data from earlier in the book, and let the user determine the X-axis, the Y-axis, the color (optional), and the size based on what the user would like to see. To do this, you will need multiple-choice menus, one for each selection the user needs to make. You can do this by following the direction written in the text book. Enjoy.

For your information:
The data is available through this link:

Add all of your libraries that you use for this assignment here.

# Add your library below.

library(shiny)
## Warning: package 'shiny' was built under R version 4.2.3
library(ggplot2)
library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Load data
rdata <- read.csv("http://www2.census.gov/programs-surveys/popest/tables/2010-2011/state/totals/nst-est2011-01.csv")

# Clean the data
data <- rdata[c(9:59,61),1:5]
# Create headers
names(data) <- c("States", "Census", "Base_Estimate", "Census_2010", "Census_2011")
data$States <- gsub("\\.", "", data$States)
# convert to numeric data
data$Census <- as.numeric(gsub(",", "", data$Census))
data$Base_Estimate <- as.numeric(gsub(",", "", data$Base_Estimate))
data$Census_2010 <- as.numeric(gsub(",", "", data$Census_2010))
data$Census_2011 <- as.numeric(gsub(",", "", data$Census_2011))

rownames(data) <- NULL

Step 1 - Build the UI

Build the UI below.

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "x",
                  label = "X-axis:",
                  choices = c("States", "Census", "Base_Estimate", "Census_2010", "Census_2011"),
                  selected = "States"),
      selectInput(inputId = "y", 
                  label = "Y-axis:", 
                  choices = c("States", "Census", "Base_Estimate", "Census_2010", "Census_2011"),
                  selected = "Census",
                  ),
      selectInput(inputId = "z", 
                  label = "Color by:", 
                  choices = c("States", "Census", "Base_Estimate", "Census_2010", "Census_2011"),
                  selected = "Estimates_Base"),
      selectInput(inputId = "s", 
                  label = "Size:", 
                  choices = c("States", "Census", "Base_Estimate", "Census_2010", "Census_2011"),
                  selected = "Census")
      ),
    # Output: Show scatterplot
    mainPanel(
      plotOutput(outputId = "scatterplot")
    )
  )
)

Step 2 - Build the Server

Build the Server below.

server <- function(input, output) {
  
 # Write your code here.
  output$scatterplot <- renderPlot({
    ggplot(data = data, aes_string(x = input$x, y=input$y, color = input$z, size = input$s)) +
      geom_point() +
      theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))
  })
  
}

Step 3 - Create the Shiny App

Deploy the shiny app.

# Write your code here.
shinyApp(ui = ui, server = server)
## 
## Listening on http://127.0.0.1:8129
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation ideoms with `aes()`