0

The function 'vectorplot' displays the gradient within a matrix, but it is not clear how you get the values of the gradient at individual pixels. As an example:

library(raster)
library(rasterVis)
ex <- matrix( c(1,2,3,4,2,3,4,5,3,4,5,6,4,5,6,7), nrow=4)
r=raster(t(ex[,ncol(ex):1]), xmn=0.5,xmx=nrow(ex)+.5, ymn=0.5,ymx=ncol(ex)+0.5)
projection(r)=CRS("+init=epsg:27700")
persp(r,theta=-50,phi=20, shade=0.23,col="red")
vectorplot(r, scaleSlope=.1)

How do you get the values of the gradient? The function 'vectorplot' returns an object, but none of the fields appears to contain information related to the gradient that is displayed. The documentation says that the display is a side-effect, so if the gradient information is not returned, is there some other package that computes the gradient and returns that information to the user?

Thanks!

Mark Bower
  • 569
  • 2
  • 16

1 Answers1

1

rasterVis uses the terrain method in the raster package

You can figure that out by following the code:

showMethods("vectorplot")  
getMethod("vectorplot", "Raster")
rasterVis:::fooSlopeAspect
rasterVis:::extractSA
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thank you for both the answer and a means for finding similar answers in the future. "The fish and teaching me to fish." :-) To get the values, I found that I had to add two more commands: 1) "sa <- terrain(r, opt = c("slope", "aspect"), unit='degrees');" 2) "as.matrix(sa)". – Mark Bower Mar 22 '18 at 16:38
  • In [this answer](https://stackoverflow.com/a/16371769/964866) you will find a detailed example about vectorplot. – Oscar Perpiñán Apr 04 '18 at 22:15