简体   繁体   English

R,传单,用颜色填充多边形

[英]R, leaflet, fill polygons with colours

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 数据是: 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 谢谢

I just happened to have that file downloaded already.. 我只是碰巧已经下载了该文件。

Your problem is that the data argument doesn't align with your fillColor parameter. 您的问题是data参数与您的fillColor参数不一致。 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. 我还删除了fillOpacity = 0.02参数,因为它使颜色变得太透明而看不到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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