r-tmap/tmaptools

NA's in smooth_map

Closed this issue · 5 comments

Hey,

Just wondering if it is possible to add an option in smooth_map to set the color for NA's..?

At the end of the smooth_map function there is a statement to set the color as the first color in the lvls:
Eg:

# just in case... values[is.na(values) | is.nan(values)] <- lvls[1]

It would be good to either leave the NA's or make them a seperate color..

Cheers

Thanks for your input. I can change it to values[is.na(values) | is.nan(values)] <- NA, but I don't know what the effect is. Normally, all values are not NA/nan. Or do you have an example for which you would like this behaviour?
The color for NA (if there is any) can be set using tmap's functions.

Thanks for taking a look... From a bit of digging I have found that it is actually kde2D that fills the whole raster with zero's instead of leaving the NA's.

The reason I notice this is when I run a spacial interpolation which might leave some areas blank due to no coverage. It would be preferable to leave those areas blank (or assign a color such as white or Transparent).

I can overcome this by converting the NA's to a value prior to running smooth_map but the neighbouring values then gets smoothed, and it provides a value for places where there should be none.

Yes, probably densities are just 0 instead of NA.

I've added a threshold argument to smooth_map. Values below that should be reported as NA:

library(tmap)
library(tmaptools)
metro_smap <- smooth_map(metro, threshold = 1e-10)
qtm(metro_smap$polygons)

Is this what you had in mind?

That is getting close.. Doesn't work too well with breaks though...
Here is some test code:

`
library(raster)
library(tmap)
library(tmaptools)

url <- "https://s3-ap-southeast-2.amazonaws.com/torbenm/data.nc"
download.file(url, destfile="data.nc")
map <- raster("data.nc")
qtm(map)

smooth = smooth_map(map, breaks=seq(0,100,10), buffer.width=1e-05, threshold=1e-05)
qtm(smooth$polygons)
`

Notice the hole in the middle of the map.

Thanks for the input. It should be working now, but please keep testing;-)