Andria Hall: Coursera Data Science Specialization
Date: November 21 2015
Coursera Data Science Specialization Department School of Public Health John Hopkins University
This Shiny app was built from the dataset counties.rds that contains demographic data for each county in the United States. It was collected using the UScensus2010 R package. This choropleth map application uses colors to display the regional variations of residents in counties across the United States who are white, black, hispanic or asian.
The dataset is in counties.rds and it contains:
A view of the demographic data for each county in the United States is shown below:
# loading the dataset using `readRDS`
counties <- readRDS("data/counties.rds")
head(counties)
name total.pop white black hispanic asian
1 alabama,autauga 54571 77.2 19.3 2.4 0.9
2 alabama,baldwin 182265 83.5 10.9 4.4 0.7
3 alabama,barbour 27457 46.8 47.8 5.1 0.4
4 alabama,bibb 22915 75.0 22.9 1.8 0.1
5 alabama,blount 57322 88.9 2.5 8.1 0.2
6 alabama,bullock 10914 21.9 71.0 7.1 0.2
This sophisticated map application was created using the shiny package. A slider widget was added that allows you to interact with the choropleth map. This widget was added to the sidebarPanel in the ui.R script below, using min = 5, max = 100, value = c(5, 100) as the Range of Interest.
ui.R
shinyUI(fluidPage(
titlePanel("USA Census App"),
sidebarLayout(
sidebarPanel(
h2("Overview"),
helpText("This shiny app creates demographic maps of the 2010 US Census."),
code('US Census Dataset:"counties.rds"'),
br(),
br(),
selectInput("var",
label = "Choose a variable to display",
choices = c("Percent White", "Percent Black",
"Percent Hispanic", "Percent Asian"),
selected = "Percent White"),
sliderInput("range",
label = "Range of interest:",
min = 5, max = 100, value = c(5, 100)),
br(),
br(),
img(src = "bigorb.png", height = 72, width = 72),
em("User-interface was made possible by:"),
span("RStudio", style = "color:blue")),
mainPanel(
h2("The US Census Features: "),
br(),
p("* The percent of residents in the county who are white, black, hispanic, or asian"),
p("* The name of each county in the United States"),
p("* The the total population of the county"),
plotOutput("map")))))
server.R
The helpers.R script shown in the R chunk below, uses the maps and the mapproj packages in R to create the choropleth map in the shiny census-app application.
library(maps)
library(mapproj)
counties <- readRDS("data/counties.rds")
source("helpers.R")
shinyServer(
function(input, output) {
output$map <- renderPlot({
data <- switch(input$var,
"Percent White" = counties$white,
"Percent Black" = counties$black,
"Percent Hispanic" = counties$hispanic,
"Percent Asian" = counties$asian)
color <- switch(input$var,
"Percent White" = "darkgreen",
"Percent Black" = "blue",
"Percent Hispanic" = "purple",
"Percent Asian" = "red")
legend <- switch(input$var,
"Percent White" = "% White",
"Percent Black" = "% Black",
"Percent Hispanic" = "% Hispanic",
"Percent Asian" = "% Asian")
percent_map(var = data,
color = color,
legend.title = legend,
max = input$range[2],
min = input$range[1]
)})})
percent_map function plots the counties data in the choropleth map. It shows the percentage of white residents in the counties in the color, dark green.
After creating a user-interactive map using the shiny ui.R and server.R script this dynamic interactive choropleth map can be viewed at [https://andriah.shinyapps.io/census-app].