I wish to create a multipanel plot (map) from several datasets ("d"
and
"q" in the example below). I can condition the main xyplot statement
on
the "site" variable, but I don't know how to pass a conditioning
variable
to panel.xyplot plot so that the x-y coordinates from dataset q are only
plotted at the appropriate site.
library(lattice)
d <- data.frame(site = c(rep("A",12), rep("B",12)),
x=rnorm(24),y=rnorm(24)) # Create dataframe
"d",
with 12 x-y coordinates for each site
q <- data.frame(site = c(rep("A",7), rep("B",7)),
x=rnorm(14),y=rnorm(14)) # Create dataframe
"q",
with 7 pairs of x-y coordinates for each site.
mypanel <- function(...){
panel.xyplot(q$x, q$y, col="red") # Statement
that
needs a "Site" conditioning variable
panel.xyplot(...)}
xyplot(y~x|site, d, panel=mypanel) # Statement erroneously plots all
14 x-y points in "q" on panels for sites A & B
Dr. Seth W. Bigelow
Biologist, USDA-FS Pacific Southwest Research Station
1731 Research Park Drive, Davis California
[[alternative HTML version deleted]]
On Thu, Mar 4, 2010 at 4:42 PM, Seth W Bigelow <sbigelow at fs.fed.us> wrote:> I wish to create a multipanel plot (map) from several datasets ("d" and > "q" in the example below). I can condition the main xyplot statement on > the "site" variable, but I don't know how to pass a conditioning variable > to panel.xyplot plot so that the x-y coordinates from dataset q are only > plotted at the appropriate site.The keyword is 'subscripts'. Look at the entry for 'panel' in ?xyplot, and let us know if you still have doubts. -Deepayan> > > library(lattice) > d <- data.frame(site ?= c(rep("A",12), rep("B",12)), > x=rnorm(24),y=rnorm(24)) ? ? ? ? ? ? ? ? ? ? ? ?# Create dataframe "d", > with 12 x-y coordinates for each site > q <- data.frame(site ?= c(rep("A",7), rep("B",7)), > x=rnorm(14),y=rnorm(14)) ? ? ? ? ? ? ? ? ? ? ? ?# Create dataframe "q", > with 7 pairs of x-y coordinates for each site. > > mypanel <- function(...){ > ? ? ? ?panel.xyplot(q$x, q$y, col="red") ? ? ? ? ? ? ? # Statement that > needs a "Site" conditioning variable > ? ? ? ?panel.xyplot(...)} > > xyplot(y~x|site, d, panel=mypanel) ? ? ?# Statement erroneously plots all > 14 x-y points in "q" on panels for sites A & B > > > > Dr. Seth ?W. Bigelow > Biologist, USDA-FS Pacific Southwest Research Station > 1731 Research Park Drive, Davis California > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
I'm stumped after an hour or so reading about subscripts in panel.xyplot.
Apparently the panel function is executed for each subset of data in the
main dataset (specified by the conditioning variable, 'site' in my
example), and the 'subscripts' keyword passes a vector of the
corresponding row numbers to the panel function. But, if I want the panel
function to simultaneously plot data from a different dataframe, as in the
example below, I don't understand how having a vector of row numbers from
a subset of the dataframe used in the main xyplot statement helps me with
selecting data from an entirely different dataframe ('q' in my example).
library(lattice)
d <- data.frame(site = c(rep("A",12), rep("B",12)),
x=rnorm(24),y=rnorm(24))
q <- data.frame(site = c(rep("A",7), rep("B",7)),
x=rnorm(14),y=rnorm(14))
mypanel <- function(...){
panel.xyplot(q$x, q$y, col="red")
panel.xyplot(...)}
xyplot(y ~ x | site, d,
panel = mypanel
)
--Seth
On Thu, Mar 4, 2010 at 4:42 PM, Seth W Bigelow <sbigelow@fs.fed.us>
wrote:> I wish to create a multipanel plot (map) from several datasets
("d" and
> "q" in the example below). I can condition the main xyplot
statement on
> the "site" variable, but I don't know how to pass a
conditioning
variable> to panel.xyplot plot so that the x-y coordinates from dataset q are only
> plotted at the appropriate site.
The keyword is 'subscripts'. Look at the entry for 'panel' in
?xyplot,
and let us know if you still have doubts.
-Deepayan
>
>
> library(lattice)
> d <- data.frame(site = c(rep("A",12), rep("B",12)),
> x=rnorm(24),y=rnorm(24)) # Create dataframe
"d",
> with 12 x-y coordinates for each site
> q <- data.frame(site = c(rep("A",7), rep("B",7)),
> x=rnorm(14),y=rnorm(14)) # Create dataframe
"q",
> with 7 pairs of x-y coordinates for each site.
>
> mypanel <- function(...){
> panel.xyplot(q$x, q$y, col="red") #
Statement that
> needs a "Site" conditioning variable
> panel.xyplot(...)}
>
> xyplot(y~x|site, d, panel=mypanel) # Statement erroneously plots
all> 14 x-y points in "q" on panels for sites A & B
>
>
>
> Dr. Seth W. Bigelow
> Biologist, USDA-FS Pacific Southwest Research Station
> 1731 Research Park Drive, Davis California
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
Ah, wonderful, thank you for the code Deepayan. To recap for posterity: I
have two datafiles, d and q: each has x-y coordinates that are conditioned
by site (The actual data, for me, is maps of parent trees and their
seedlings). I wanted to superimpose the xy plots of d and q, by site,
without going to the trouble of merging the d & q datasets into a single
dataset. The solution is to use the "which.packet" statement is
d <- data.frame(site = c(rep("A",12), rep("B",12)),
x=rnorm(24),y=rnorm(24)) # Create the main xy dataset
q <- data.frame(site = c(rep("A",7), rep("B",7)),
x=rnorm(14),y=rnorm(14)) # Create the alternate xy dataset
q.split <- split(q, q$site) # Split up the alternate
dataset by site
mypanel <- function(..., alt.data) {
with(alt.data[[ which.packet()[1] ]], #
which.packet passes index of the relevant data subset...
panel.xyplot(x = x, y = y, col="red")) # ...
to
panel.xyplot()
panel.xyplot(...)
}
xyplot(y ~ x | site, d, alt.data = q.split, # After providing
the alternative dataset and the panel...
panel = mypanel) # ...everything prints out
properly, like magic!
Dr. Seth W. Bigelow
Biologist, USDA-FS Pacific Southwest Research Station
[[alternative HTML version deleted]]
Alternatively library(latticeExtra) xyplot(y ~ x | site, d) + xyplot(y ~ x | site, q, col = "red") (which is a shortcut for:) xyplot(y ~ x | site, d) + as.layer(xyplot(y ~ x | site, q, col = "red")) On 9 March 2010 11:17, Seth W Bigelow <sbigelow at fs.fed.us> wrote:> Ah, wonderful, thank you for the code Deepayan. To recap for posterity: I > have two datafiles, d and q: each has x-y coordinates that are conditioned > by site (The actual data, for me, is maps of parent trees and their > seedlings). I wanted to superimpose the xy plots of d and q, by site, > without going to the trouble of merging the d & q datasets into a single > dataset. The solution is to use the "which.packet" statement is > > > d <- data.frame(site ?= c(rep("A",12), rep("B",12)), > x=rnorm(24),y=rnorm(24)) ? ? ? ? ? ? ? ?# Create the main xy dataset > q <- data.frame(site ?= c(rep("A",7), rep("B",7)), > x=rnorm(14),y=rnorm(14)) ? ? ? ? ? ? ? ?# Create the alternate xy dataset > > > q.split <- split(q, q$site) ? ? ? ? ? ? ? ? ? ? # Split up the alternate > dataset by site > > mypanel <- function(..., alt.data) { > ? ?with(alt.data[[ which.packet()[1] ]], ? ? ? ? ? ? ? ? ? ? ? # > which.packet passes index of the relevant data subset... > ? ? ? ? panel.xyplot(x = x, y = y, col="red")) ? ? ? ? ? ? ? ? # ... to > panel.xyplot() > ? ?panel.xyplot(...) > } > > xyplot(y ~ x | site, d, alt.data = q.split, ? ? ? ? ? ? # After providing > the alternative dataset and the panel... > ? ? ? panel = mypanel) ? ? ? ? ? ? ? ? ? ? ? ? # ...everything prints out > properly, like magic! > > > > Dr. Seth ?W. Bigelow > Biologist, USDA-FS Pacific Southwest Research Station > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Felix Andrews / ??? Postdoctoral Fellow Integrated Catchment Assessment and Management (iCAM) Centre Fenner School of Environment and Society [Bldg 48a] The Australian National University Canberra ACT 0200 Australia M: +61 410 400 963 T: + 61 2 6125 4670 E: felix.andrews at anu.edu.au CRICOS Provider No. 00120C -- http://www.neurofractal.org/felix/