1

I have a raster file, which I created from data downloaded from DIVA-GIS: http://www.diva-gis.org/datadown

nz_map<-raster("NZL1_msk_cov.grd")

Using plot() on this object works great, so there are no issues importing it. The raster object contains a lot of data I don't need, data on land cover. I want a more simple raster object with lon & lat coordinates and a value of 1 for land and NA for ocean.

This raster will be used with the dismo function randomPoints() to sample background data for modelling species distribution, so the most important thing is to identify which areas are land(suitable for sampling) and which are ocean(unsuitable).

I can visualise the raster more simply with plot(!is.na(nz_map5)). This works well and services for the randomPoints() function, but I'm not sure how to edit the color of the map. Doing this: plot(!is.na(nz_map5), col="grey") results in a totally grey block, instead of just colouring the appropriate areas grey; this is why I thought I might be better off with a more simple raster object, to do away with the !is.na argument Any ideas?

If anyone knows of a place you could download such files, saving me the hassle- that works, too.

clinaaeus
  • 11
  • 2

1 Answers1

0

Here are similar data for elevation

library(raster)
a <- getData("alt", country="NZL")
r <- a[[1]]
plot(r)

I think your confusion stems from what happens here

x <- !is.na(r)

That turns the values to TRUE (those that were not NA) or FALSE (those that were NA). So now you have two categories

plot(x, col=c("red", "blue"))

And now it is no longer a good dataset for dismo::randomPoints

If you would rather have NA and 1 other value you can do

y <- r * 0
plot(y, col="blue")

Or

 y <- reclassify(y, cbind(-Inf, Inf, 1))

But, as you say yourself, for randomPoints you can just use the original data.

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63