R Leaflet Map Polygon Labels on highlight have incorrect label

谁都会走 提交于 2020-08-25 03:55:30

问题


I am creating a map in R using leaflet and the labels for my polygons are not lining up with what their value should be.

The map is meant to separate just Ontario by FSA (Forward Sortation Area), the geojson includes all the FSAs in Canada.

So first I subset just the FSAs for Ontario (Using their first letter as this is how FSAs work).


file_path <- "data/regions.geojson"

fsa <- geojsonio::geojson_read(
  x=file_path,
  what="sp"
)

#Get subsets
fsa_ont <-subset (
  x=fsa,
  subset= grepl(
    x=fsa@data$CFSAUID,
    pattern="^L|^M|^P|^K|^N"  

  )
)

The map is also supposed to highlight each FSA by the number of people from that FSA who are in the data. I create a colour palette based on the count of people by the FSA. The colour palette seems to work based on the labels it ends up giving the polygons.

#create a colour palette

mypalette <- colorBin(palette=c("#8acb88", "#FF9933", "#990033"),  domain= pop$Count, bins=c(1,5,10,15),  na.color='white')

Next a create the map tiles

map <- leaflet::leaflet() %>%
  leaflet::addProviderTiles(providers$OpenStreetMap)

Then I create the text for my label.

This was a label to check what was going wrong. It should be showing the FSA pulling from the geojson data, then the FSA it is pulling from my data set, and then the count from the data set.

The count and the FSA from my data set match each-other on each label, but they do not match the FSA from the geojson. Neither FSA matches what the real name of the polygon should be.

mytext=paste("FSA Ont Data: ", fsa@data$CFSAUID, "FSA Ours:", pop$FSA,"Count: ", pop$Count)

This is creating the map with my polygons, seems to work well.

map_fsa<- map %>%
  leaflet::addPolygons(
    data=fsa_ont,
    weight=1,
    opacity=3,
    color="white",
    dashArray="3",
  ##  fillColor= ~mypalette(pop$Count),
  ##  fillOpacity=.7,
    highlight = highlightOptions(
      weight = 2,
      color = "#666",
      dashArray = "",
      fillOpacity = 4,
      bringToFront = TRUE
      ),
    label= mytext)%>%
  addLegend("bottomright", pal = mypalette, values = pop$Count,
                        title = "Survey Respondents",
                        opacity = 1)

map_fsa

Picture of output, and a polygon label Picture of Northern Ontario FSAs This is the output of the code: See how the label not only doesn't match the correct FSA for the region but the geojson and my data do not seem to match either. The colour does seem to be correct.

Please give me some guidance!

Edit: I've looked up solutions to this and the closet i've found was this question but I cannot seem to make it work for my code.


回答1:


I fixed the problem!

The problem was fsa_ont and pop were mismatching so I was able to merge them and use them as one.

I merged them together and stored them in the same data set using the following after subsetting fsa_ont:

colnames(pop)[colnames(pop)== "FSA"]<-"CFSAUID" ##make column names the same

fsa_all <- merge(
  fsa_ont@data, pop, by="CFSAUID" ##merge based on their common column
)

fsa_ont@data <-fsa_all ## put this merged data set into the map data

Then I was able to use my new column in the dataframe fsa_ont@data$Count as the fill variable and everything fell into place

map <- leaflet::leaflet() %>%
  leaflet::addProviderTiles(providers$OpenStreetMap)



#create a colour palette with handmade bins
mypalette <- colorBin(palette=c("#8acb88", "#FF9933", "#990033"),  domain= fsa_ont@data$Count, bins=c(1,5,10,15),  na.color='white')
mytext=paste("FSA Ont Data: ", fsa_ont@data$CFSAUID, "FSA Ours:", fsa_all$CFSAUID,"Count: ",fsa_ont@data$Count ) ## This 'FSA Ont Data:' and 'FSA Ours' should match

#Add the FSA polygons to map with correct colouring
map_fsa<- map %>%
  leaflet::addPolygons(
    data=fsa_ont,
    weight=1,
    opacity=3,
    color="white",
    dashArray="3",
    fillColor= ~mypalette(fsa_ont@data$Count),
    fillOpacity=.7,
    highlight = highlightOptions(
      weight = 2,
      color = "#666",
      dashArray = "",
      fillOpacity = 4,
      bringToFront = TRUE
    ),
    label= ~mytext)%>%
  addLegend("bottomright", pal = mypalette, values = fsa_ont@data$Count,
            title = "Survey Respondents",
            opacity = 1)

map_fsa

and here is the beautiful map Map working with labels matching!

I am still not sure why my original code doesn't work - so I would love any answers on that still.



来源:https://stackoverflow.com/questions/55288438/r-leaflet-map-polygon-labels-on-highlight-have-incorrect-label

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!