3

I want to produce a .png of a raster with background values plotted as white space.

Below you can see the current state of the plot.

As you can tell, the pixels which should contain NA values, in fact register as a single value of 94. The issue is that running a simple replace via r[ r== 94] <- NA would also conceivably replace points within the raster. Also it's a 21GB raster, so this method would be computationally onerous.

I've produced four other .pngs from the same raster stack without this issue, as png(raster.png); plot(raster); dev.off() automatically produced white space for background values. Do you have any other suggestions for dealing with this, outside of a crop or something in photoshop?

cti plot

1

It seems like the band you are plotting does not have the NA values correctly specified, and has a value of 94 instead, otherwise its hard to explain how you get the above plot result. So you have to clean that data layer.

One option to do that could be to make an overlay with another band from the same raster. If you have another layer otherband with correctly specified NA values with the same "structure" as r (i.e. the NA values in another band are the same as in the one you show above), you could overlay those two.

rna <- overlay(r, otherband, fun=function(x,y){if(is.na(y)) return(NA) else return(x)})

If you want to try the conversion instead, I would suggest reclassification. The raster package has a memory efficient function for reclassification of rasters. Your could replace the value 94 with a NA value with the reclassify function.

rna <- reclassify(r, cbind(94, NA))
  • Does this work in reverse? If I want to assign a dummy value to NA values, could I use rev<-reclassify(rna, cbind(NA, 999))? – user3386170 Apr 4 '18 at 14:41
  • I guess that should work - why don't you try it out? – yellowcap Apr 4 '18 at 16:53
  • reclassify did work in reverse. I also notice subs for which the following is detailed: You could obtain the same result with reclassify, but subs is more efficient for simple replacement. Use reclassify if you want to replace ranges of values with new values. But all values were replaced with a value (not just NAs)... (although it is generally much, much faster than reclassify for simple replacement and it would be great to speed things up for NA replacement too) – user3386170 Apr 5 '18 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.