简体   繁体   中英

Choropleth map colors are missing

I am trying to create a Choropleth map for Iowa Counties. I downloaded the map data from https://geodata.iowa.gov/dataset/county-boundaries-iowa/resource/183e782f-2d43-4073-8524-fe8e634cf17a

I do get a map, but it is uniform instead of the color I chose from the R brewer palletes nor is it differentiated by data -

library(shiny)
library(shinydashboard)
library(ggplot2)
library(DT)
library(plotly)
library(leaflet)
library(sf)
library(geojsonio)


mymap = st_read("county.shp")%>% st_transform(crs = 4326)

bins <- c((mean(mymap$AREA)-2*sd(mymap$AREA)), (mean(mymap$AREA)-1*sd(mymap$AREA)), (mean(mymap$AREA)),(mean(mymap$AREA)+1*sd(mymap$AREA)),(mean(mymap$AREA)+2*sd(mymap$AREA)))
pal <- colorBin("YlOrRd", domain = mymap$AREA, bins = bins)

bins_1 <- c((mean(mymap$PERIMETER)-2*sd(mymap$PERIMETER)), (mean(mymap$PERIMETER)-1*sd(mymap$PERIMETER)), (mean(mymap$PERIMETER)),(mean(mymap$PERIMETER)+1*sd(mymap$PERIMETER)),(mean(mymap$PERIMETER)+2*sd(mymap$PERIMETER)))
pal_1 <- colorBin("YlOrRd", domain = mymap$PERIMETER, bins = bins_1)
######################Shiny Dashboard############################################
sidebar <- dashboardSidebar(
)

body <- dashboardBody(
  leafletOutput("myplot")
)

ui <- dashboardPage(
  dashboardHeader(title = "My shiny app"),
  sidebar = sidebar,
  body = body
)

server <- function(input, output, session) {
output$myplot = renderLeaflet({leaflet(mymap)%>%addProviderTiles("CartoDB")%>%
    addTiles(group = "OSM (default)")%>%addPolygons(fill = 0, weight = 1, color = "#000000",group = "Base Map")%>%
    addPolygons(fill = ~pal(AREA), weight = 1,group = "Area")%>%
    addLegend(pal = pal, values = ~AREA, opacity = 0.7, title = NULL,
              position = "bottomright")%>%
    addPolygons(fill = ~pal_1(PERIMETER), weight = 1,group = "Perimeter")%>%
    addLegend(pal = pal_1, values = ~PERIMETER, opacity = 0.7, title = NULL,
              position = "bottomright")%>%
    addLayersControl(baseGroups = c("Base Map", "Area","Perimeter"))})
}


shinyApp(ui, server)

The problem is your second use of addPolygons .
It needs to have color = ~pal(AREA) , not fill = ~pal(AREA) .

fill is TRUE or FALSE (default TRUE) whether or not they should be filled with your colors.
color is the actual color/colors to fill with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM