In R using the raster package, you can create box plots from two rasters (x and y) like so:



x = Raster* object y = If x is a RasterLayer object, y can be an additional RasterLayer to group the values of x by 'zone'

Which works perfectly when I want to group the values of x by all the classes in my thematic raster y.

But if I only want to group the values in x by a subset of the thematic classes in y, is there a way to do this? I could replace any values in y outside the values I am interested with NODATA values, but I was hoping there a cleaner, simpler method.

1 Answer 1


I found that the value matching operator %in% works perfectly, to replace raster values I don't want (e.g., 4, 7, 10) with NA, and then I can call the boxplot method again.

y[y%in%c(4,7,10)] <- NA
  • you can also use thee subs or reclassify function Oct 27, 2015 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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