Anthony Darrouzet-Nardi
2004-May-29 20:24 UTC
[R] panel function in a conditioned lattice graphic
I'm trying to use plotting character to encode the variable "block" from my dataset in a conditioned lattice graphic (R 1.9.0 on Mac OS 10.3.3). The data I'm using is the dataframe "dryoutcover" which is here (4k): anthony.darrouzet-nardi.net/downloads/dryoutcover.Rdata The code that generates my graphic almost correctly is as follows: xyplot(coversage ~ dryout | year, data=dryoutcover, panel = function(x,y) { panel.lmline(x,y) one <- dryoutcover$block==1 two <- dryoutcover$block==2 thr <- dryoutcover$block==3 fou <- dryoutcover$block==4 fiv <- dryoutcover$block==5 six <- dryoutcover$block==6 grid.points(x[one], y[one], pch=49) grid.points(x[two], y[two], pch=50) grid.points(x[thr], y[thr], pch=51) grid.points(x[fou], y[fou], pch=52) grid.points(x[fiv], y[fiv], pch=53) grid.points(x[six], y[six], pch=54) } ) The only thing wrong is that this does not correctly encode blocks 5 and 6, which only appear in the third year of the study, 2003 (the third panel of the graphic). It instead labels blocks 5 and 6 as blocks 1 and 2 as if another year had started over (but on the same panel). For example, the point farthest to the right in the third panel says "2" when I want it to say "6." When I do not condition by year, it correctly displays blocks 5 and 6. How can I correct this in the conditioned graphic? Anthony Darrouzet-Nardi
On Saturday 29 May 2004 15:24, Anthony Darrouzet-Nardi wrote:> I'm trying to use plotting character to encode the variable "block" > from my dataset in a conditioned lattice graphic (R 1.9.0 on Mac OS > 10.3.3). The data I'm using is the dataframe "dryoutcover" which is > here (4k): > > anthony.darrouzet-nardi.net/downloads/dryoutcover.Rdata > > The code that generates my graphic almost correctly is as follows: > > xyplot(coversage ~ dryout | year, > data=dryoutcover, > panel = function(x,y) { > panel.lmline(x,y) > one <- dryoutcover$block==1 > two <- dryoutcover$block==2 > thr <- dryoutcover$block==3 > fou <- dryoutcover$block==4 > fiv <- dryoutcover$block==5 > six <- dryoutcover$block==6 > grid.points(x[one], y[one], pch=49) > grid.points(x[two], y[two], pch=50) > grid.points(x[thr], y[thr], pch=51) > grid.points(x[fou], y[fou], pch=52) > grid.points(x[fiv], y[fiv], pch=53) > grid.points(x[six], y[six], pch=54) > } > ) > > The only thing wrong is that this does not correctly encode blocks 5 > and 6, which only appear in the third year of the study, 2003 (the > third panel of the graphic). It instead labels blocks 5 and 6 as > blocks 1 and 2 as if another year had started over (but on the same > panel). For example, the point farthest to the right in the third > panel says "2" when I want it to say "6." When I do not condition by > year, it correctly displays blocks 5 and 6. > > How can I correct this in the conditioned graphic?This probably happens because things like `one <- dryoutcover$block==1` are logical vectors as long as the total number of rows in the data frame, whereas x and y (which you subset using x[one], y[one]) are shorter (only those rows for a particular year). Consequently, the vector you are indexing and the indexing vector are not comparable. I would suggest you use the feature meant for this sort of display, namely as: xyplot(coversage ~ dryout | year, data=dryoutcover, groups = block, pch = c(49:54)) Hope that helps, Deepayan
Anthony Darrouzet-Nardi
2004-May-30 01:18 UTC
[R] panel function in a conditioned lattice graphic
>On Saturday 29 May 2004 15:24, Anthony Darrouzet-Nardi wrote: > > I'm trying to use plotting character to encode the variable "block" > > from my dataset in a conditioned lattice graphic (R 1.9.0 on Mac OS > > 10.3.3). The data I'm using is the dataframe "dryoutcover" which is > > here (4k): > > > > anthony.darrouzet-nardi.net/downloads/dryoutcover.Rdata > > > > The code that generates my graphic almost correctly is as follows: > > > > xyplot(coversage ~ dryout | year, > > data=dryoutcover, > > panel = function(x,y) { > > panel.lmline(x,y) > > one <- dryoutcover$block==1 > > two <- dryoutcover$block==2 > > thr <- dryoutcover$block==3 > > fou <- dryoutcover$block==4 > > fiv <- dryoutcover$block==5 > > six <- dryoutcover$block==6 > > grid.points(x[one], y[one], pch=49) > > grid.points(x[two], y[two], pch=50) > > grid.points(x[thr], y[thr], pch=51) > > grid.points(x[fou], y[fou], pch=52) > > grid.points(x[fiv], y[fiv], pch=53) > > grid.points(x[six], y[six], pch=54) > > } > > ) > > > > The only thing wrong is that this does not correctly encode blocks 5 > > and 6, which only appear in the third year of the study, 2003 (the > > third panel of the graphic). It instead labels blocks 5 and 6 as > > blocks 1 and 2 as if another year had started over (but on the same > > panel). For example, the point farthest to the right in the third > > panel says "2" when I want it to say "6." When I do not condition by > > year, it correctly displays blocks 5 and 6. > > > > How can I correct this in the conditioned graphic? > >This probably happens because things like `one <- dryoutcover$block==1` >are logical vectors as long as the total number of rows in the data >frame, whereas x and y (which you subset using x[one], y[one]) are >shorter (only those rows for a particular year). Consequently, the >vector you are indexing and the indexing vector are not comparable. > >I would suggest you use the feature meant for this sort of display, >namely as: > >xyplot(coversage ~ dryout | year, > data=dryoutcover, groups = block, pch = c(49:54)) > >Hope that helps, > >DeepayanOh yes, that does indeed help. I should have thought of using "groups". And more importantly, thanks for explaining why the indexing method was not working. I have a followup question. Suppose I want to encode two different variables within a panel: one variable encoded by plotting character and one variable encoded by symbol color (as if I could use two "groups" variables). The dataframe I discussed above also includes a variable called treatment. If I start with the existing code modified with your suggestions: xyplot(coversage ~ dryout | year, data = dryoutcover, groups = block, panel = function(x,y, ...) { panel.lmline(x,y) panel.superpose(x,y, pch = 49:54, cex = rep(2,6), col = rep("black", 6), ...) } ) how could I make all of the symbols of one treatment red and all of the symbols of the other black while maintaining the encodings of block by plotting character? This would be a superbly useful technique as it would allow 4 dimensional data on a single panel (maybe even 5 using a point cloud!). Anthony
Gabor Grothendieck
2004-May-31 16:44 UTC
[R] Calculating distances between points in a data frame?
Try using running from the gregmisc package with pad = TRUE: require(gregmisc) XY <- data.frame(num = seq(0,10), X = seq(0,30,3), Y = seq(0, 40, 4) ) DistXY <- function(idx) { i <- idx[2] with(XY, sqrt( (X[i]-X[i-1])^2 + (Y[i]-Y[i-1])^2 ) ) } XY$Dist <- running( 1:nrow(XY), width=2, fun = DistXY, pad = TRUE ) Sander Oom <slist <at> oomvanlieshout.net> writes: : : Dear list, : : I would like to calculate the distance between consecutive points in a data : frame. Of course the first point in the data frame does not have a point of : origin, and should get a value NA. I have tried two different loops, which : both result in error: : : > num <- seq(0,10,1) : > X <- seq(0,30,3) : > Y <- seq(0,40,4) : > XY <- data.frame(num, X, Y) : > attach(XY) : > summary(XY) : num X Y : Min. : 0.0 Min. : 0.0 Min. : 0 : 1st Qu.: 2.5 1st Qu.: 7.5 1st Qu.:10 : Median : 5.0 Median :15.0 Median :20 : Mean : 5.0 Mean :15.0 Mean :20 : 3rd Qu.: 7.5 3rd Qu.:22.5 3rd Qu.:30 : Max. :10.0 Max. :30.0 Max. :40 : > plot(X,Y) : > rngNum <- range(num) : > for (i in rngNum){ : + XY$DistXY[i] <- sqrt( ((X[i]-X[i-1])^2) + ((Y[i]-Y[i-1])^2) ) : + } : Error in "$<-.data.frame"(`*tmp*`, "DistXY", value = sqrt(((X[i] - X[i - : : replacement has 10 rows, data has 11 : > for (i in rngNum){ : + XY$DistXY2[i] <- ifelse(i=min(rngNum), NA, sqrt(((X[i]-X[i-1])^2) + : ((Y[i]-Y[i-1])^2)) ) : + } : Error in ifelse(i = min(rngNum), NA, sqrt(((X[i] - X[i - 1])^2) + ((Y[i] - : : unused argument(s) (i ...) : > detach(XY) : > : : Any suggestions much appreciated, : : Sander Oom. : : -------------------------------------------------------------- : Dr. Sander P. Oom : Animal, Plant and Environmental Sciences : University of the Witwatersrand : Private Bag 3 : Wits 2050 : South Africa : : Tel (work) +27 (0)11 717 64 04 : Tel (home) +27 (0)18 297 44 51 : Fax +27 (0)18 299 24 64 : : Email sander <at> oomvanlieshout.net : Web oomvanlieshout.net/sander : : ______________________________________________ : R-help <at> stat.math.ethz.ch mailing list : stat.math.ethz.ch/mailman/listinfo/r-help : PLEASE do read the posting guide! R-project.org/posting-guide.html : :