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:
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!