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)
library(ggplot2)
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(rsconnect)
##
## Attaching package: 'rsconnect'
## The following object is masked from 'package:shiny':
##
## serverInfo
rsconnect::setAccountInfo(name='screws',
token='A96553C5177AE77F76E6FCE0E845C937',
secret='QMoW2EJBWQufJbjxFcrPi78c1ZxKiGaD0fvyGtzX')
Build the UI below.
ui <- fluidPage(
titlePanel("2010-2011 Census"),
sidebarLayout(
sidebarPanel(
#select variables for y-axis
selectInput(inputId = "y",
label = "Y-axis",
choices = c("july population" = "july11pop",
"region of country" = "region",
"change in popluation" = "popChange",
"Percent change in popujlation" = "percentChange"),
selected = "region"),
#Select variables for x-axis
selectInput(inputId = "x",
label = "X-axis",
choices = c("july population" = "july11pop",
"region of country" = "region",
"change in popluation" = "popChange",
"Percent change in population" = "percentChange"),
selected = "jully11pop"),
#Set alpha level
sliderInput(inputId = "alpha",
label = "Alpha",
min = 0, max = 1,
value = 0.5),
#Select variable for color
selectInput(inputId = "z",
label = "Color by:",
choices = c("july population" = "july11pop",
"region of country" = "region",
"change in popluation" = "popChange",
"Percent change in popujlation" = "percentChange"),
selected = "popChange"),
),
#Output: Show scatterplot
mainPanel(
plotOutput(outputId = "scatterplot"))))
Build the Server below.
#Define server function
server <- function(input, output) {
dfStates <- readCensus()
dfStates <- dfStates[dfStates$stateName != "District of Columbia",]
dfStates$region <- state.region
dfStates$stateName <- tolower(dfStates$stateName)
dfStates$popChange <- dfStates$july11pop - dfStates$july10pop
dfStates$percentChange <- dfStates$popChange/dfStates$july10pop * 100
output$scatterplot <- renderPlot({
ggplot(dfStates, aes_string(x = input$x, y = input$y, color = input$z)) +
geom_point(alpha = input$alpha)
})
}
#read in the census data set
readCensus <- function(){
urlToRead <-
"http://www2.census.gov/programs-surveys/popest/tables/2010-2011/state/totals/nst-est2011-01.csv"
#clean data set
testFrame <- read.csv(url(urlToRead))
testFrame <- testFrame[-1:-8,]
testFrame <- testFrame[,1:5]
testFrame$stateName <- testFrame[,1]
testFrame <- testFrame[,-1]
testFrame <- testFrame[-52:-58,]
testFrame$stateName <- gsub("\\.","",testFrame$stateName)
#convert columns to numbers
testFrame$april10census <- gsub(",", "", testFrame$X)
testFrame$april10base <- gsub(",", "", testFrame$X.1)
testFrame$july10pop <- gsub(",", "", testFrame$X.2)
testFrame$july11pop <- gsub(",", "", testFrame$X.3)
testFrame$april10census <- as.numeric(gsub(" ", "", testFrame$april10census))
testFrame$april10base <- as.numeric(gsub(" ", "", testFrame$april10base))
testFrame$july10pop <- as.numeric(gsub(" ", "", testFrame$july10pop))
testFrame$july11pop <- as.numeric(gsub(" ", "", testFrame$july11pop))
testFrame <- testFrame[,-1:-4]
#remove the row names
rownames(testFrame) <- NULL
return(testFrame)
}
Deploy the shiny app.
# Write your code here.
shinyApp(ui = ui, server = server)