Reputation: 3608
I have shape data for London, and want to colour different regions red, yellow, and green.
My code works, but does not fill it according to red, yellow, or green.
The data is: https://data.london.gov.uk/dataset/statistical-gis-boundary-files-london
Here is my code:
library("rgdal")
library(leaflet)
shapeData <- readOGR('statistical-gis-boundaries-london/ESRI/LSOA_2004_London_Low_Resolution.shp')
shapeData <- spTransform(shapeData, CRS("+proj=longlat +ellps=GRS80"))
LANAME='Camden'
shapeData$col=sample(c('red','yellow','green'),nrow(shapeData),1)
leaflet() %>% addTiles() %>%
setView(lng = -0.106, lat=51.5177,zoom=14) %>%
addPolygons(data=bor,weight=2,col = 'black',fillOpacity = 0.02,fillColor = shapeData$col,
highlightOptions = highlightOptions(color='white',weight=1,
bringToFront = TRUE)) %>%
addMarkers(lng = -0.106,lat=51.5177,popup="Hi there")
The output is:
Can anyone point out why I do not see yellow, green, or red only, and see all these other colours as well.
Thanks
Upvotes: 3
Views: 9665
Reputation: 3986
I just happened to have that file downloaded already..
Your problem is that the data
argument doesn't align with your fillColor
parameter. Instead you should run:
leaflet() %>% addTiles() %>%
setView(lng = -0.106, lat=51.5177,zoom=14) %>%
addPolygons(data=shapeData,weight=2,col = 'black',fillColor = shapeData$col,
highlightOptions = highlightOptions(color='white',weight=1,
bringToFront = TRUE)) %>%
addMarkers(lng = -0.106,lat=51.5177,popup="Hi there")
I also removed the fillOpacity = 0.02
argument as it was making the colours far too transparent to see.
Upvotes: 4