0

I am trying to create a map of the UK with a colorscale indicating the yield for a particular crop in each region. I have got the plot to work and have even managed to get some marker data in there but for some reason leaflet is just randomly allocating colors and not actually doing it based on the yield.

This is the code I am using, I combine merged the yield data (see below) into the read .geojson data called mapviz, into mapviz@data:

#Designates a color palette and spreads it across the crop yield range
pal <- colorNumeric(palette = "Greens", domain = mapviz$Yield)
  
# Creates the map using the "leaflet" command
map <- leaflet(mapviz) %>%
    addTiles()%>%
    addLabelOnlyMarkers(mapviz$long, mapviz$lat, 
                        label =  ~as.character(mapviz$Yield), 
                        labelOptions = labelOptions(noHide = T, 
                        direction = 'top', textOnly = T)) %>%
    addProviderTiles("Esri.WorldGrayCanvas") %>%
    addPolygons(stroke = TRUE, color = "Black", weight="1", 
                fillOpacity = 1, fillColor = ~pal(Yield))%>%
    addLegend(pal=pal, values = mapviz$Yield,
              labFormat = labelFormat(suffix = mapsuffix), 
              opacity = 1, title = maplegend, position = "topright")

and this is the data:

                     Region    lat    long Yield
1             East Midlands 52.600 -0.8130  6.30
2                   Eastern 52.000  0.4395  6.99
3                North East 55.000 -2.0435  6.45
4                North West 54.300 -2.9004  6.88
5                  Scotland 56.505 -4.1528  7.08
6                South East 51.000 -0.7031  7.00
7                South West 50.700 -2.5000  7.14
8                     Wales 52.000 -3.5000  5.92
9             West Midlands 52.300 -2.3621  6.16
10 Yorkshire and The Humber 53.700 -1.2854  7.60

but it creates a map that looks like this: enter image description here

You can see that the numbers and colors don't match up. I have checked the raw data and the numbers are correct! Please let me know if you have any ideas or if I have forgotten to give you any information!

dezza
  • 15
  • 3
  • To be absolutely sure that we are seeing the *real* input data, please edit your question to include the output from `dput(mapvis)`. Thank you. – Limey Jul 20 '20 at 13:48
  • Second idea. From the leaflet documentation for `addLegend`; values the values used to generate colors from the palette function. Do you need `values = sort(mapviz$Yield)` or `labels= sort(mapviz$Yield)`? – Limey Jul 20 '20 at 13:53
  • @Limey Do you mean dput(map)? If so it is 1000's of lines long as "map" also contains all the polygon data for the outlines. – dezza Jul 20 '20 at 13:57
  • @Limey If I try to run without ```values = sort(mapviz$Yield)``` in the ```addlegend``` I get: ```Error in evalFormula(values, data) : argument "values" is missing, with no default``` – dezza Jul 20 '20 at 13:59
  • I mean the data that you print in your question, with Region = "East Midlands" on the first line in your question. – Limey Jul 20 '20 at 14:07
  • @Limey find it here: https://github.com/djschep/cropmap – dezza Jul 20 '20 at 14:12

1 Answers1

0

I managed to fix this! It turns out that leaflet doesn't automatically assign the colors based on an id. It instead does it based on the order the regions appear in the .geojson file. I therefore fixed it by changing the order the data was in so that it matched the order the regions show up in the .geojson file.

I can explain more if someone else has this issue :)

dezza
  • 15
  • 3