...Hello,
On the basis of R codes in Shiny Tutorial Lesson 5, I am using the following R codes to create a map in Shiny. I am interested in Ohio's counties and therefore I filtered data using subset command. I have two questions:
1- How can I add the name of counties on the map?
2- How can I increase the size of the map.
Thanks,
Nader
library(shiny)
library(dplyr)
if(!require(maps)) stop("This app requires the maps package.\nPlease install it and then try again.")
if(!require(mapproj)) stop("This app requires the maps package.\nPlease install it and then try again.")
counties <- readRDS("counties.RDS")
counties <- subset(counties, region== "ohio")
mp <- map("county", plot=FALSE, namesonly=TRUE)
c.order <- match(mp,
paste(counties$region, counties$subregion, sep = ","))
shinyServer(function(input, output) {
indexInput <- reactive({
var <- switch(input$var,
"Total Population (logged)" = log(counties$pop),
"Percent White" = counties$white,
"Percent Black" = counties$black,
"Percent Hispanic" = counties$hispanic,
"Percent Asian" = counties$asian)
var <- pmax(var, input$range[1])
var <- pmin(var, input$range[2])
as.integer(cut(var, 100, include.lowest = TRUE,
ordered = TRUE))[c.order]
})
shadesInput <- reactive({
switch(input$var,
"Percent White" = colorRampPalette(c("white", "darkgreen"))(100),
"Percent Black" = colorRampPalette(c("white", "black"))(100),
"Percent Hispanic" = colorRampPalette(c("white", "darkorange3"))(100),
"Percent Asian" = colorRampPalette(c("white", "darkviolet"))(100))
})
legendText <- reactive({
inc <- diff(range(input$range)) / 4
c(paste0(input$range[1], " % or less"),
paste0(input$range[1] + inc, " %"),
paste0(input$range[1] + 2 * inc, " %"),
paste0(input$range[1] + 3 * inc, " %"),
paste0(input$range[2], " % or more"))
})
output$mapPlot <- renderPlot({
fills <- shadesInput()[indexInput()]
map("county", fill = TRUE, col = fills,
resolution = 100, lty = 0, projection="polyconic",
myborder = 0, mar = c(0,0,0,0))
map("state",col = "white", fill=FALSE, add=TRUE, lty=1,
lwd=1,projection="polyconic", myborder = 0,
mar = c(0,0,0,0))
legend("bottomleft", legend = legendText(),
fill = shadesInput()[c(1, 25, 50, 75, 100)],
title = input$var)
})
})