Created
April 4, 2017 20:21
-
-
Save leonallen22/70dc57269389e059e72f6febb4d69916 to your computer and use it in GitHub Desktop.
Data Science Assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Note: percent map is designed to work with the counties data set | |
# It may not work correctly with other data sets if their row order does | |
# not exactly match the order in which the maps package plots counties | |
percent_map <- function(var, color, legend.title, min = 0, max = 100) { | |
# generate vector of fill colors for map | |
shades <- colorRampPalette(c("white", color))(100) | |
# constrain gradient to percents that occur between min and max | |
var <- pmax(var, min) | |
var <- pmin(var, max) | |
percents <- as.integer(cut(var, 100, | |
include.lowest = TRUE, ordered = TRUE)) | |
fills <- shades[percents] | |
# plot choropleth map | |
map("county", fill = TRUE, col = fills, | |
resolution = 0, lty = 0, projection = "polyconic", | |
myborder = 0, mar = c(0,0,0,0)) | |
# overlay state borders | |
map("state", col = "white", fill = FALSE, add = TRUE, | |
lty = 1, lwd = 1, projection = "polyconic", | |
myborder = 0, mar = c(0,0,0,0)) | |
# add a legend | |
inc <- (max - min) / 4 | |
legend.text <- c(paste0(min, " % or less"), | |
paste0(min + inc, " %"), | |
paste0(min + 2 * inc, " %"), | |
paste0(min + 3 * inc, " %"), | |
paste0(max, " % or more")) | |
legend("bottomleft", | |
legend = legend.text, | |
fill = shades[c(1, 25, 50, 75, 100)], | |
title = legend.title) | |
} | |
getCountyDemo = function(df, select) | |
{ | |
switch(select, | |
"Percent White" = "white", | |
"Percent Black" = "black", | |
"Percent Hispanic" = "hispanic", | |
"Percent Asian" = "asian") | |
} | |
getFilteredVals = function(vect, maxP, minP) | |
{ | |
filtVals = vect[vect<maxP] | |
filtVals = filtVals[filtVals>minP] | |
if(all(is.na(filtVals))) | |
{ | |
c(0,0) | |
} | |
filtVals | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(maps) | |
library(mapproj) | |
source("helpers.R") | |
function(input, output) { | |
counties = readRDS("data/counties.rds") | |
output$map <- renderPlot({ | |
cols = getCountyDemo(counties, input$var) | |
vals = counties[[cols]] | |
filtVals = getFilteredVals(vals, input$range[2], input$range[1]) | |
percent_map(filtVals, "darkgreen", "Legend") | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
# Define UI for application that draws a histogram | |
fluidPage( | |
# Application title | |
titlePanel("censusVis"), | |
# Sidebar with a slider input for the number of bins | |
sidebarLayout( | |
sidebarPanel( | |
helpText("Create demographic maps with info from | |
the 2010 US Census."), | |
selectInput("var", | |
label = "Choose a variable to display", | |
choices = c("Percent White", "Percent Black", | |
"Percent Hispanic", "Percent Asian"), | |
selected = "Percent White"), | |
sliderInput("range", | |
"Range of interests", | |
min = 0, | |
max = 100, | |
value = c(0, 100)) | |
), | |
# Show a plot of the generated distribution | |
mainPanel( | |
plotOutput("map") | |
) | |
) | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment