Héctor Villalobos
2009-Jan-19 19:51 UTC
[R] plotting arrows with different colors and varying head size
Dear list, I would like to plot arrows with different colors according to arrow length, and also (if possible) with head size proportional to arrow length. The idea is to make a quiver-like plot of matlab with wind speed data. So far, I´ve been able to use different colors, but I need to find a more efficient way to recode arrow length intervals into colors. On the contrary, I can't define different head sizes, because the "length" argument in the "arrows()" function seems to control the head size of all the arrows at once. Any help will be greatly appreciated. ## Generate random data set.seed(1) xcoor <- matrix(runif(20), ncol=5) ycoor <- matrix(runif(20), ncol=5) u <- matrix(rnorm(20)/10, ncol=5) v <- matrix(rnorm(20)/10, ncol=5) ## calculate arrows length and look histogram arrowlen <- sqrt(u^2 + v^2) hist(arrowlen) ## recode arrow lengths (by interval) into colors ## sorry, I don't know how to do it without the car package library(car) arrowL <- recode(as.vector(arrowlen), "0='grey90'; 0:0.05='grey50'; 0.05:0.1='grey'; 0.1:0.15='cyan'; 0.15:0.2='blue'; 0.2:0.25='red'") length=0.1 par.uin <- function() # determine scale of inches/userunits in x and y # from http://tolstoy.newcastle.edu.au/R/help/01c/2714.html # Brian Ripley Tue 20 Nov 2001 - 20:13:52 EST { u <- par("usr") p <- par("pin") c(p[1]/(u[2] - u[1]), p[2]/(u[4] - u[3])) } ## plot arrows plot(as.vector(xcoor), as.vector(ycoor), type="p", pch=".", xlim=c(-0.2, 1.3), ylim=c(-0.2, 1.3)) arrows( xcoor, ycoor, xcoor + u, ycoor + v, length = length*as.vector(arrowlen)*min(par.uin()), col=arrowL) > sessionInfo() R version 2.8.1 (2008-12-22) i386-pc-mingw32 locale: LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods [7] base other attached packages: [1] car_1.2-9 hdf5_1.6.7 -- Héctor Villalobos <hvillalo@ipn.mx> CICIMAR - IPN La Paz, Baja California Sur, MÉXICO [[alternative HTML version deleted]]
Greg Snow
2009-Jan-19 21:45 UTC
[R] plotting arrows with different colors and varying head size
Look at the my.symbols function in the TeachingDemos package (along with the ms.arrows function in the same package), that may do what you want. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of H?ctor Villalobos > Sent: Monday, January 19, 2009 12:51 PM > To: r-help at r-project.org > Subject: [R] plotting arrows with different colors and varying head > size > > Dear list, > > I would like to plot arrows with different colors according to arrow > length, and also (if > possible) with head size proportional to arrow length. The idea is to > make a quiver-like plot of > matlab with wind speed data. > > So far, I?ve been able to use different colors, but I need to find a > more efficient way to recode > arrow length intervals into colors. On the contrary, I can't define > different head sizes, because > the "length" argument in the "arrows()" function seems to control the > head size of all the > arrows at once. > > Any help will be greatly appreciated. > > > > ## Generate random data > set.seed(1) > xcoor <- matrix(runif(20), ncol=5) > ycoor <- matrix(runif(20), ncol=5) > u <- matrix(rnorm(20)/10, ncol=5) > v <- matrix(rnorm(20)/10, ncol=5) > > ## calculate arrows length and look histogram > arrowlen <- sqrt(u^2 + v^2) > hist(arrowlen) > > ## recode arrow lengths (by interval) into colors > ## sorry, I don't know how to do it without the car package > library(car) > arrowL <- recode(as.vector(arrowlen), > "0='grey90'; > 0:0.05='grey50'; > 0.05:0.1='grey'; > 0.1:0.15='cyan'; > 0.15:0.2='blue'; > 0.2:0.25='red'") > > length=0.1 > > par.uin <- function() > # determine scale of inches/userunits in x and y > # from http://tolstoy.newcastle.edu.au/R/help/01c/2714.html > # Brian Ripley Tue 20 Nov 2001 - 20:13:52 EST > { > u <- par("usr") > p <- par("pin") > c(p[1]/(u[2] - u[1]), p[2]/(u[4] - u[3])) > } > > > ## plot arrows > plot(as.vector(xcoor), as.vector(ycoor), type="p", pch=".", xlim=c(- > 0.2, 1.3), > ylim=c(-0.2, 1.3)) > arrows( xcoor, ycoor, xcoor + u, ycoor + v, > length = length*as.vector(arrowlen)*min(par.uin()), > col=arrowL) > > > > > sessionInfo() > R version 2.8.1 (2008-12-22) > i386-pc-mingw32 > > locale: > LC_COLLATE=English_United States.1252;LC_CTYPE=English_United > States.1252;LC_MONETARY=English_United > States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252 > > attached base packages: > [1] stats graphics grDevices utils datasets methods > [7] base > > other attached packages: > [1] car_1.2-9 hdf5_1.6.7 > > > > -- > H?ctor Villalobos <hvillalo at ipn.mx> > CICIMAR - IPN > La Paz, Baja California Sur, M?XICO > > > [[alternative HTML version deleted]]
Jim Lemon
2009-Jan-20 11:13 UTC
[R] plotting arrows with different colors and varying head size
H?ctor Villalobos wrote:> Dear list, > > I would like to plot arrows with different colors according to arrow length, and also (if > possible) with head size proportional to arrow length. The idea is to make a quiver-like plot of > matlab with wind speed data. > > So far, I?ve been able to use different colors, but I need to find a more efficient way to recode > arrow length intervals into colors. On the contrary, I can't define different head sizes, because > the "length" argument in the "arrows()" function seems to control the head size of all the > arrows at once. >Hi Hector, The color.scale function in the plotrix package may do what you want, also have a look at the vectorField function. I think you will have to draw the arrows one at a time or rewrite the arrows function to get different head sizes. Jim