File: vectorField.R

package info (click to toggle)
r-cran-plotrix 3.8-4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,588 kB
  • sloc: makefile: 6
file content (37 lines) | stat: -rwxr-xr-x 1,277 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Arguments:
# u,v - the x (longitude) and y (latitude) offsets
# OR orientation and magnitude
# xpos,ypos - the centers of the vectors
# scale - the longest arrow as a proportion of the cell size
# headspan - the extent of the arrowhead as a proportion of cell size
# this function doesn't assume a 1:1 aspect ratio

vectorField<-function (u, v, xpos = NA, ypos = NA, scale = 1, headspan = 0.1, 
    vecspec = c("lonlat", "rad", "deg"), col = par("fg")) 
{
    udim <- dim(u)
    if (is.na(xpos[1])) 
        xpos <- col(u)
    if (is.na(ypos[1])) 
        ypos <- udim[1] - row(u) + 1
    ymult <- getYmult()
    if (match(vecspec[1], "lonlat", 0) == 0) {
        if (match(vecspec[1], "deg", 0)) 
            u <- pi * u/180
        mag <- v
        tempu <- v * cos(u)
        v <- v * sin(u) * ymult
        u <- tempu
    }
    else mag <- sqrt(u * u + v * v)
    if (is.null(dim(xpos))) 
        maxmag <- 0.5 * max(diff(as.numeric(xpos)))/max(mag)
    else maxmag <- 0.5 * max(diff(as.numeric(xpos[1, ])))/max(mag)
    u2 <- u * scale * maxmag
    v2 <- v * scale * maxmag
    if (is.null(udim)) 
        length = headspan
    else length <- headspan * par("pin")[1]/udim[2]
    arrows(xpos - u2, ypos - v2, xpos + u2, ypos + v2, length = length, 
        col = col)
}