Jialun
Jialun

Reputation: 31

Leaflet map color appear lighter

I'm trying to make a simple map with Leaflet in R Markdown. I used standard deviation classification method to divide the data into 4 classes. When it produces the map, the colors on the map appear much lighter than what they should be. What is the problem here ? Thank you!

library(rgdal)
library(leaflet)
library(readr)
STCOU<- readOGR("H:/SP/STCOU.shp", "STCOU", GDAL1_integer64_policy = TRUE)
UNGEOCODED <- read_csv("H:/SP/UNGEOCODED.csv")

palette1 <- colorBin(c('#f1eef6','#bdc9e1','#74a9cf','#0570b0'),bins = c(0,1600, 3400, 5200, 32000),pretty = FALSE)

palette2 <- colorBin(c('#ece7f2','#a6bddb','#2b8cbe'),bins = c(0,1816, 3821, 35781),pretty = FALSE)

popup1<-paste("2015 Ungeocoded Tallies",
         "<br>GEO_ID: ",
         STCOU$GEOID,
         "<br>State: ",
         STCOU$STATEFP,
         "<br>County: ",
         STCOU$COUNTYFP,
         "<br>Name: ",
         STCOU$NAME,
         "<br>LSAD: ",
         STCOU$LSAD,
         "<br>CensusArea: ",
         STCOU$ALAND,
         "<br>Current_UNGEO_Counts: ",
         STCOU$CURRENT_UN,
         "<br>Fall15_UNGEO_Counts: ",
         STCOU$FAL15_UNGE)

popup2<-paste("2016 Ungeocoded Tallies",
         "<br>GEO_ID: ",
         STCOU$GEOID,
         "<br>State: ",
         STCOU$STATEFP,
         "<br>County: ",
         STCOU$COUNTYFP,
         "<br>Name: ",
         STCOU$NAME,
         "<br>LSAD: ",
         STCOU$LSAD,
         "<br>CensusArea: ",
         STCOU$ALAND,
         "<br>Current_UNGEO_Counts: ",
         STCOU$CURRENT_UN,
         "<br>Fall15_UNGEO_Counts: ",
         STCOU$FAL16_UNGE)
```

### Ungeocoded 2015 2016

```{r}
leaflet() %>%
setView(lng = -94.308561, lat = 38.561161, zoom=5) %>%
addProviderTiles("Esri.WorldGrayCanvas",
               options = tileOptions()) %>%
addPolygons(data=STCOU,weight=0.1,
          fillColor = ~palette1(UNGEOCODED$FAL15_UNGE),
          popup = popup1,
          group="<span style='color: #7f0000; font-size: 11pt'><strong>2015</strong></span>")%>%
addPolygons(data=STCOU,weight=0.1,
          fillColor = ~palette2(UNGEOCODED$SPR16_UNGE),
          popup=popup2,
          group="2016") %>%
addLayersControl(baseGroups = c("<span style='color: #7f0000; font-size: 11pt'><strong>2015</strong></span>",
               "2016"), options = layersControlOptions(collapsed =FALSE))%>%
addLegend(position = 'topleft',
       colors = c('#f1eef6','#bdc9e1','#74a9cf','#0570b0'), 
       labels = c('Low'," "," ","High"),
       opacity = 0.6,
       title = "2015 2016 Ungeocoded")
```

Upvotes: 0

Views: 581

Answers (1)

cjtexas
cjtexas

Reputation: 41

Basically you just need to read the docs on the various addLayers functions in leaflet package and you'll see that fillOpacity is set to 0.2 as the default. This is to save you from yourself when features overlap, but you can definitely set the parameter higher, i.e.

Minimal Reproducible Example + Answer -->

library(leaflet)

df <- data.frame(
  lon = sample(seq(-94.9, -93.1, 0.00001), 10, replace = T),
  lat = sample(seq(38.1, 39.9, 0.00001), 10, replace = T),
  value = sample(density(c(0,1600, 3400, 5200, 32000))$x, 10)
) 

palette1 <- colorBin(c('#f1eef6','#bdc9e1','#74a9cf','#0570b0'), bins = c(0,1600, 3400, 5200, 32000), pretty = FALSE)

leaflet(df) %>%
  setView(lng = -94.308561, lat = 38.561161, zoom=7) %>%
  addProviderTiles("Esri.WorldGrayCanvas") %>% 
  addCircleMarkers(
    weight=0.1, 
    fillOpacity = 0.7,
    fillColor = ~palette1(value)
  )

Upvotes: 0

Related Questions