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 programming style above would also be welcome. 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 theS 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi All. I have two questions about "coherency" in R versus Splus: 1) Why plot x$coh surrounded by confidence intervals based on sqrt(x$coh)? Why not just plot sqrt(x$coh) surrounded by ci's based on sqrt(x$coh) OR plot x$coh surrounded by ci's based on x$coh? [A function I had used to plot squared coherency using Splus output does it the second way, while R (and Bloomfield 2000) seem to plot x$coh surrounded by ci's based on sqrt(x$coh).] 2) I think that spectrum() in Splus (6.0 Release 1 for Linux 2.2.12) and spectrum() in R 1.4.0 (rev 2001-11-19) do not produce the same numbers for their estimates of coherency although both claim to produce "squared coherency" in their help files [and R clearly does so on line 72 of spec.pgram()]. I ran the following test on both setups (using Linux, kernel 2.4): t<-1:240 #Monthly Time for 20 Years y1<-2*cos(2*pi*t*5/240)+3*sin(2*pi*t*5/240) #Series with a 4 year cycle y2<-.5*y1+rnorm(length(t)) #Second series y1ts<-ts(y1,freq=12) y2ts<-ts(y2,freq=12) Y<-ts.union(y1ts,y2ts) specy<-spectrum(Y,plot=FALSE,spans=c(3,3),taper=.1,demean=FALSE,detrend=FALSE) #Write out the results to file: #with: dput(specy$coh,file="specycohR.r") in R #with: dput(specy$coh,file="specycohS.s") in S specycohS<-dget("specycohS.s") #Get the Splus version into R. specycohR<-specy$coh #I know that Splus includes freq=0 and R does not so I exclude the #first entry in the Splus vector. Rcompare<-sapply(list(Sversion=specycohS[-1],Rversion=specycohR), FUN=function(x){ c(length=length(x),mean=mean(x),sd=sd(x),quantile(x))}) round(Rcompare,5) Sversion Rversion length 120.00000 120.00000 mean 0.23751 0.28726 sd 0.22376 0.26523 0% 0.00061 0.00266 25% 0.07786 0.07150 50% 0.17793 0.22233 75% 0.31521 0.44021 100% 0.99619 1.22397 #I also plotted the two vectors against one another, and did not see #any easily discernible pattern [i.e. it doesn't look like one is a #simple function of the other.] But when I plotted both against #x$freq, both did get the relationship at freq=.25 [4 year cycle], #but otherwise they looked quite different. For now I'm going to trust the R version for both the calculation of ci's for x$coh AND for the calculation of x$coh itself. But, it would set my mind at ease to know what is going on. Does anyone know? Thanks very much! Jake Bowers Dept of Political Science UC-Berkeley -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._