r-spatial / mapview

Interactive viewing of spatial data in R
https://r-spatial.github.io/mapview/
GNU General Public License v3.0
519 stars 90 forks source link

mismatched colors in legend for raster (similar to issue 362 but unresolved for raster?) #439

Open geocaruso opened 2 years ago

geocaruso commented 2 years ago

Hello and thanks for this great package, so useful to explore maps while developing a piece of code

I encounter a problem, which I believe is similar to 362, which was resolved, but maybe not for raster layers?

I have a raster layer with 3 values: 2001,2008,2019

image

If I run mapview with 3 colours: red for 2001, blue for 2008 and green for 2019, I get a legend with interpolated colour (purple) for the 2001 value. The other two colours being correct on the map.

image

The warning indeed informs me I have 4 values but there is actually only 3 (all zeroes were previously sent to NA and are thus transparent)

unique(urbanyear) [1] 2001 2008 2019

I can obtain the map I want if I add a fourth colour, i.e. yellow in this example, but then the legend no longer corresponds. The last one (green) is not in

image

Looks like mapview believes there is another, smaller ,value in the raster, but there are none, which the legend seems to get correct in terms of values, not colours

(sorry if this not std process for reporting, this is actually 1st time ever I report an issue with github) Thanks for great work and any help Geoffrey

tim-salabim commented 2 years ago

What happens if you add method = "ngb" to the mapview call?

geocaruso commented 2 years ago

No difference.

image
tim-salabim commented 2 years ago

Ok, I think the problem is that you actually want a factorial raster rather than a numeric one. Can you provide the data for me to look into it deeper?

geocaruso commented 2 years ago

Sure. You should be able to download then load the raster as an R obj (to avoid conversion to tif) from here https://www.dropbox.com/s/grqn2w0ttw3x3e9/urbanyear?dl=0

tim-salabim commented 2 years ago

Thanks. I am travelling today so will probably not be able to look at it until tomorrow

geocaruso commented 2 years ago

Have a safe trip! Thanks!