File: vectorField.R

package info (click to toggle)
r-cran-plotrix 3.2-6-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,136 kB
  • sloc: makefile: 3
file content (31 lines) | stat: -rwxr-xr-x 1,036 bytes parent folder | download
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
# 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")) {
 udim<-dim(u)
 if(is.na(xpos[1])) xpos<-col(u)
 if(is.na(ypos[1])) ypos<-udim[1]-row(u)+1
 # if long/lat not specified
 if(match(vecspec[1],"lonlat",0) == 0) {
  # convert the degrees to radians if necessary
  if(match(vecspec[1],"deg",0)) u<-pi*u/180.
  mag<-v
  tempu<-v*cos(u)
  v<-v*sin(u)
  u<-tempu
 }
 else mag<-sqrt(u*u+v*v)
 if(is.null(dim(xpos))) maxmag<-0.5*max(diff(xpos))/max(mag)
 else maxmag<-0.5*max(diff(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)
}