Skip to content

Plotting direction vectors from an aspect map

8 messages · Jonathan Greenberg, Dylan Beaudette, Robert J. Hijmans +5 more

#
R-sig-geo'ers:

Given an aspect raster (GDAL-readable), is there a way to plot the
aspect image using arrows representing each cell's direction?  I'm
assuming I have to convert the raster to a set of points -- are there
arrow symbols and, if so, can you rotate them based on some parameter?

Thanks!

--j
#
For doing this within R also have a look at ggplot2, with a simple sin 
and cos one could use geom_segment

http://had.co.nz/ggplot2/geom_segment.html

Bart
On 09/16/2011 04:22 AM, Dylan Beaudette wrote:
#
I've done something similar for my own work (based on polygon
centroids, but the idea is the same). The following (messy) code
should do what you want using base graphics. The inputs are the
coordinates of your cell centers, the angles of your arrows, and then
a few additional parameters which control size and shape etc.

Hope it can be of some use?

Carson

point.arrows <- function (coords, angles, size = 0.08, arrow = FALSE,
                          length=0.05, point = FALSE, point.col=NULL, ...)
{
    is.xy <- (is.list(coords) && all(c("x", "y") %in% names(coords)))
    xy <- if (is.xy) {
        coords
    }
    else xy.coords(coords[,1], coords[,2])
    x <- xy$x
    y <- xy$y
    n <- length(x)

    if (point) points(x, y, col=point.col, ...)
    ppin <- par("pin")
    pusr <- par("usr")

    if (length(size) == n)
        sizes <- size/max(size)
    else
        sizes <- size[1]

    xr <- sizes * abs(pusr[2L] - pusr[1L])/ppin[1L]
    yr <- sizes * abs(pusr[4L] - pusr[3L])/ppin[2L]
    z <- numeric()
    deg <- angles
    if (arrow)
        arrows(x, y, x + xr * sin(deg), y + yr * cos(deg), length, ...)
    else
        segments(x, y, x + xr * sin(deg), y + yr * cos(deg), ...)
}

On Fri, Sep 16, 2011 at 2:17 AM, Jonathan Greenberg
<greenberg at ucdavis.edu> wrote:

  
    
#
For adding arrows, look at the my.symbols and ms.arrows functions in the TeachingDemos package.
1 day later
#
Hi,

I have uploaded a first draft of a vectorplot function in rasterVis. It
is available here:
https://r-forge.r-project.org/scm/viewvc.php/drafts/vectorplot.R?view=markup&root=rastervis.
It uses the raster::slopeAspect function to calculate the slope/aspect
from a raster object. 

For example:

library(raster)
library(rasterVis)

df <- expand.grid(x=seq(-2, 2, .1), y=seq(-2, 2, .1))
df$z <- with(df, (3*x^2 + y)*exp(-x^2-y^2))
r1 <- rasterFromXYZ(df)
df$z <- with(df, x*exp(-x^2-y^2))
r2 <- rasterFromXYZ(df)
df$z <- with(df, y*exp(-x^2-y^2))
r3 <- rasterFromXYZ(df)

projection(r1) <- projection(r2) <- projection(r3) <-
CRS("+proj=longlat +datum=WGS84")

vectorplot(r1, par.settings=RdBuTheme)
vectorplot(r2, par.settings=RdBuTheme)
vectorplot(r3, par.settings=RdBuTheme)

I will include it in the rasterVis package after some tests.

Best,

Oscar.

-------------
Oscar Perpi??n Lamigueiro
Dpto. de Ingenier?a El?ctrica
EUITI-UPM

http://procomun.wordpress.com