[R] quiver plot help

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 20 10:13:52 CET 2001


On Tue, 20 Nov 2001, Robin Hankin wrote:

> Hello everybody
>
> I'm trying to write a simple version of matlab's "quiver".
>
> The idea is that I have fluid with velocity defined on a grid.  I have
> a matrix of x-components of velocity and a matrix of y-components and
> I want to see the overall flow pattern.  (I work with 2D fluid
> mechanics problems).
>
> My first-stab function is below:
>
> quiver  <- function(u,v,scale=1)
> # first stab at matlab's quiver in R
>   {
>     xpos <- col(u)
>     ypos <- max(row(u))-row(u)
>
>     speed <- sqrt(u*u+v*v)
>     maxspeed <- max(speed)
>
>     u <- u*scale/maxspeed
>     v <- v*scale/maxspeed
>
>     matplot(xpos,ypos,type="p",cex=0)
>     arrows(xpos,ypos,xpos+u,ypos+v,length=0.05)
>   }
>
>
> Thus, for example
>
> u <- matrix(rnorm(100),nrow=10)
> v <- matrix(rnorm(100),nrow=10)
> quiver(u,v)
>
>
>
> This works (after a fashion), but I want to do two things:
>
> (1) scale the arrows so that the longest of them is the same length as
>     the distance between two adjacent points plotted by matplot
>
> (2) scale the arrow head to (say) one tenth of the length of its
>     arrow.
>
> I can't see how to do either of these because "length" argument to
> arrows specifies the length of the edges of the arrow head in inches,
> not in the units of the axes.  Also, any tips on how to improve the

S has a par("uin") telling you the number of inches per axes units.
R does not, but

uin <- function()
{
    u <- par("usr")
    p <- par("pin")
    c(p[1]/(u[2] - u[1]), p[2]/(u[4] - u[3]))
}

reproduces that information.  You can then compute arrow lengths in device
space, which should be all you need.

> programming style above would also be welcome.

Adding some spaces (around +, after ,) would help readability.

> thanks in advance
>
>
> robin
>
> --
>
> Robin Hankin, Lecturer,
> School of Environmental and Marine Science
> Private Bag 92019 Auckland
> New Zealand
>
> r.hankin at auckland.ac.nz
> tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list