Marius Hofert
2011-Feb-08 11:36 UTC
[R] "strange" behavior of panel.abline inside a for-loop
Dear expeRts, I would like to create a list of lattice xyplots. Here is the minimal example: library(lattice) plot.list <- vector("list", 10) for(i in 1:10){ plot.list[[i]] <- xyplot(i~i, type="p", xlim=c(0,11), panel=function(...){ panel.xyplot(...) panel.abline(v=i) }) } plot.list[[3]] As you can see, the vertical line is *always* printed at x=10 [and not at x=i]. Why? Cheers, Marius
Karl Ove Hufthammer
2011-Feb-08 12:39 UTC
[R] "strange" behavior of panel.abline inside a for-loop
Marius Hofert wrote:> library(lattice) > plot.list <- vector("list", 10) > for(i in 1:10){ > plot.list[[i]] <- xyplot(i~i, type="p", xlim=c(0,11), panel=function(...){ > panel.xyplot(...) > panel.abline(v=i) > }) > } > plot.list[[3]] > > As you can see, the vertical line is always printed at x=10 [and not at > x=i]. Why?No, it?s printed at x=i, which is 10 at the end of the loop. You can see this by changing i to for example 5 and rerunning ?plot.list[[3]]?. -- Karl Ove Hufthammer
Marius Hofert
2011-Feb-08 14:28 UTC
[R] "strange" behavior of panel.abline inside a for-loop
Dear Karl, thanks for your answer. I am still wondering why the point "i~i" is plotted at "i" but not the vertical line? And how I can plot the vertical line at "i". Do you know a solution? Cheers, Marius
Peter Ehlers
2011-Feb-08 15:22 UTC
[R] "strange" behavior of panel.abline inside a for-loop
On 2011-02-08 06:28, Marius Hofert wrote:> Dear Karl, > > thanks for your answer. > > I am still wondering why the point "i~i" is plotted at "i" but not the vertical line? And how I can plot the vertical line at "i". Do you know a solution?Marius, try this: plot.list <- vector("list", 3) for(i in 1:3){ plot.list[[i]] <- xyplot(i~i, type="p", xlim=c(0,11), panel=function(x, y,...){ panel.xyplot(x, y,...) panel.abline(v = x) }) } plot.list[[1]] Peter Ehlers> > Cheers, > > Marius > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Marius Hofert
2011-Feb-08 15:55 UTC
[R] "strange" behavior of panel.abline inside a for-loop
Dear Peter, thanks for your response. Since in my original problem, the "abline" depends on i [but neither on x nor y], I tried something like this: library(lattice) plot.list <- vector("list", 4) for(i in 1:4){ plot.list[[i]] <- xyplot(1~1, type="p", xlim=c(0,11), panel=function(x, y, i, ...){ panel.xyplot(x, y, ...) panel.abline(v = sqrt(i)) }) } plot.list[[2]] How can I pass through the "i"? It should be possible to draw a line with a parameter depending on "i". Cheers, Marius On 2011-02-08, at 16:22 , Peter Ehlers wrote:> On 2011-02-08 06:28, Marius Hofert wrote: >> Dear Karl, >> >> thanks for your answer. >> >> I am still wondering why the point "i~i" is plotted at "i" but not the vertical line? And how I can plot the vertical line at "i". Do you know a solution? > > Marius, try this: > > plot.list <- vector("list", 3) > for(i in 1:3){ > plot.list[[i]] <- xyplot(i~i, type="p", xlim=c(0,11), > panel=function(x, y,...){ > panel.xyplot(x, y,...) > panel.abline(v = x) > }) > } > plot.list[[1]] > > Peter Ehlers > >> >> Cheers, >> >> Marius >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >
rex.dwyer at syngenta.com
2011-Feb-08 20:11 UTC
[R] "strange" behavior of panel.abline inside a for-loop
Dear Marius, Try this: plot.list = lapply(1:10, function(i) xyplot(i~i,type="p",xlim=c(0,11),panel=function(...) { panel.xyplot(...); panel.abline(v=i)}) ) plot.list[[3]] I imagine it will work for Mr Luftjammer, too. Rex -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Marius Hofert Sent: Tuesday, February 08, 2011 6:37 AM To: Help R Subject: [R] "strange" behavior of panel.abline inside a for-loop Dear expeRts, I would like to create a list of lattice xyplots. Here is the minimal example: library(lattice) plot.list <- vector("list", 10) for(i in 1:10){ plot.list[[i]] <- xyplot(i~i, type="p", xlim=c(0,11), panel=function(...){ panel.xyplot(...) panel.abline(v=i) }) } plot.list[[3]] As you can see, the vertical line is *always* printed at x=10 [and not at x=i]. Why? Cheers, Marius ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited.
Marius Hofert
2011-Feb-08 20:22 UTC
[R] "strange" behavior of panel.abline inside a for-loop
Yep, that finally does it. Many thanks, Rex. Cheers, Marius On 2011-02-08, at 21:11 , <rex.dwyer at syngenta.com> <rex.dwyer at syngenta.com> wrote:> Dear Marius, > Try this: > > plot.list = lapply(1:10, > > function(i) xyplot(i~i,type="p",xlim=c(0,11),panel=function(...) { > panel.xyplot(...); panel.abline(v=i)}) > ) > plot.list[[3]] > > I imagine it will work for Mr Luftjammer, too. > Rex > > -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Marius Hofert > Sent: Tuesday, February 08, 2011 6:37 AM > To: Help R > Subject: [R] "strange" behavior of panel.abline inside a for-loop > > Dear expeRts, > > I would like to create a list of lattice xyplots. Here is the minimal example: > > library(lattice) > plot.list <- vector("list", 10) > for(i in 1:10){ > plot.list[[i]] <- xyplot(i~i, type="p", xlim=c(0,11), panel=function(...){ > panel.xyplot(...) > panel.abline(v=i) > }) > } > plot.list[[3]] > > As you can see, the vertical line is *always* printed at x=10 [and not at x=i]. Why? > > Cheers, > > Marius > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > > > > message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited. >