Michael Braun
2008-Aug-16 04:39 UTC
[R] Lattice: problem using panel.superpose and panel.groups
Hi. I'm embarking on my first attempt at creating my own panel function for lattice graphics, and despite all of my online research and pouring through the documentation, I cannot figure out how to solve my particular problem. Hopefully, a generous fellow R user can help. I have some data that is split into two groups: some "actual" data, and some simulated data, generated from several different models. The actual data come from two different datasets (calibration and holdout), and the simulations were calibrated on each data set under the various models. What I want to do is create a boxplot on the simulated data, and superimpose a line representing the actual data. This plot would condition on dataset and model. While the simulated data various across model and dataset, the actual data varies only across dataset and is common for all models. My approach was to use panel.superpose, and create a panel function that, depending on the group.number, calls either panel.bwplot or panel.lines. Like this: panel.custom.plot <- function (..., group.number) { if (group.number == 1) { panel.bwplot(...) } else { panel.lines(...) } } obj <- bwplot(p ~ as.factor(count) | dataset + model, data = data.frame, panel = panel.superpose, groups = group, panel.groups = panel.custom.plot } When I do this, I see the bwplot, but I cannot see the lines. It is as though the data isn't being passed through. Can someone help me get started with this, and instruct me a little bit more on how arguments are passed from trellis functions to the panel functions? I did consider a second approach, where I don't use panel.superpose or groups at all, but pass the actual.calib and actual.hold datasets as parameters to a panel function. This panel function would call panel.bwplot, and then, depending on the value of dataset, add lines for either actual.calib or actual.hold. But I do not know how to let the panel function know what level of a conditioning variable is being plotted in a particular panel. Thanks in advance for any help you can provide, Michael Michael Braun Homer A. Burnell Career Development Professor and Assistant Professor of Management Science (Marketing Group) MIT Sloan School of Management One Amherst St., E40-169 Cambridge, MA 02139 (617) 253-3436 braunm at mit.edu
Dieter Menne
2008-Aug-16 08:14 UTC
[R] Lattice: problem using panel.superpose and panel.groups
Michael Braun <braunm <at> MIT.EDU> writes:> > I have some data that is split into two groups: some "actual" data, > and some simulated data, generated from several different models. The > actual data come from two different datasets (calibration and > holdout), and the simulations were calibrated on each data set under > the various models. > > What I want to do is create a boxplot on the simulated data, and > superimpose a line representing the actual data. This plot would > condition on dataset and model. While the simulated data various > across model and dataset, the actual data varies only across dataset > and is common for all models. >Michael, to understand you problem, I started creating a data set that simulates the data, but I got stuck with with your intended meaning of "groups. df = data.frame(p=rnorm(100),count=rpois(100,5),dataset=c("A","B"), model=rep(c("M1","M2"),each=50),groups= dont' know) Could you please (as the posting guide says) supply a self-running example that shows the problem? You named your data.frame "data.frame"; it could work, but better don't use common things like "c" or "data.frame" as variable name. Dieter
Michael Braun
2008-Aug-16 18:12 UTC
[R] Lattice: problem using panel.superpose and panel.groups
Dieter: Thank you for your response. As you requested, I created a self- running example, pasted below. It may be a little wordier than I would like, but it runs. The data I am creating assumes that there are two datasets ("calib" and "hold"), with actual values actual.calib and actual.hold. I then simulate data for three models, A, B and C. I want to plot a 3 x 2 lattice that has bwplots for each model-dataset combination in each panel. On top of that, I want to superimpose a line that plots the actual data for calib in all three panels in the calib column (and the same for hold). The data.frame sim.data is just the simulated data, and actual.data is just the actual data. all.data combines them both, and the group variable identifies whether a record is from "sim" or "actual." Note that there are 50 simulations, but only one actual, for each model- dataset combination. The presentation in all.data is kind of wasteful, since I replicated the same actual data for all 3 models. So, ideally, I would work with sim.data and actual.data separately. But I can use all.data if necessary. I apologize that I was not more claear in my earlier post, but hopefully this gives you enough information. Thanks again for helping with this. Michael library(lattice) # creating datasets models <- c("A","B","C") datasets <- c("calib","hold") counts <- 1:10 n.sims <- 50 sim.data <- NULL all.data <- NULL actual.calib <- seq(0,5,length=10) actual.hold <- seq(5,0,length=10) cols <- c("model","dataset","count","value","group") actual.data <- as.data.frame(cbind(c(rep("calib",10),rep("hold",10)), c(1:10,1:10),c(actual.calib,actual.hold))) set.names <- c(rep("calib",n.sims),rep("hold",n.sims)) for (mod in models) { for (y in counts) { x.calib <- rnorm(n=n.sims,mean=actual.calib[y],sd=1) x.hold <- rnorm(n=n.sims,mean=actual.hold[y],sd=1) x <- as.data.frame(cbind(rep(mod,2*n.sims), set.names, y, c(x.calib,x.hold),rep("sim",2*n.sims))) colnames(x) <- cols sim.data <- rbind(sim.data, x) } rep.actual <- as.data.frame(cbind(rep(mod,20), actual.data,rep("actual",20))) colnames(rep.actual) <- cols all.data <- rbind(all.data,sim.data,rep.actual) } colnames(actual.data) <- c("dataset","count","value") # for some reason, value is encoded as a factor. # This changes value to numeric. # (is there a better way to do this?) sim.data[,"value"] <- as.numeric(levels(sim.data[,"value"]) [sim.data[,"value"]]) all.data[,"value"] <- as.numeric(levels(all.data[,"value"]) [all.data[,"value"]]) actual.data[,"value"] <- as.numeric(levels(actual.data[,"value"]) [actual.data[,"value"]]) # at this point, there are three data frames worth considering: # sim.data - simulated data # actual.data - actual data # all.data - sim.data, and the actual.data replicated for each model # create panel function panel.ppc.plot <- function(...,group.number) { if (group.number==1) { panel.bwplot(...) } else { panel.lines(...) } } # create and plot lattice object obj <- bwplot(as.numeric(value) ~ as.factor(count) | dataset + model, data = all.data, panel = panel.superpose, groups=group, panel.groups = panel.ppc.plot ) plot(obj)> > > > ------------------------------ > > Message: 119 > Date: Sat, 16 Aug 2008 08:14:36 +0000 (UTC) > From: Dieter Menne <dieter.menne at menne-biomed.de> > Subject: Re: [R] Lattice: problem using panel.superpose and > panel.groups > To: r-help at stat.math.ethz.ch > Message-ID: <loom.20080816T080831-746 at post.gmane.org> > Content-Type: text/plain; charset=us-ascii > > Michael Braun <braunm <at> MIT.EDU> writes: > >> >> I have some data that is split into two groups: some "actual" data, >> and some simulated data, generated from several different models. >> The >> actual data come from two different datasets (calibration and >> holdout), and the simulations were calibrated on each data set under >> the various models. >> >> What I want to do is create a boxplot on the simulated data, and >> superimpose a line representing the actual data. This plot would >> condition on dataset and model. While the simulated data various >> across model and dataset, the actual data varies only across dataset >> and is common for all models. >> > Michael, > > to understand you problem, I started creating a data set that > simulates the > data, but I got stuck with with your intended meaning of "groups. > > df = data.frame(p=rnorm(100),count=rpois(100,5),dataset=c("A","B"), > model=rep(c("M1","M2"),each=50),groups= dont' know) > > Could you please (as the posting guide says) supply a self-running > example that > shows the problem? > > You named your data.frame "data.frame"; it could work, but better > don't use > common things like "c" or "data.frame" as variable name. > > Dieter > >
Dieter Menne
2008-Aug-17 09:36 UTC
[R] Lattice: problem using panel.superpose and panel.groups
Michael Braun <braunm <at> MIT.EDU> writes:> > Dieter: > > Thank you for your response. As you requested, I created a self- > running example, pasted below. It may be a little wordier than I > would like, but it runs... Details removed> > panel.ppc.plot <- function(...,group.number) { > > if (group.number==1) { > panel.bwplot(...) > } else { > > panel.lines(...) > } > }Trellis graphics are a bit like hash functions: you can be close to the target, but get a far-off result. I admit that I do not know why panel.lines does not work in the above example, but panel.polygon(...) works in your special case of ordered data. More generally, I would suggest to use panel.xyplot(x,y,type="l") or, if you want the ... notation, panel.xyplot(...) but then you have to set type="l" in your bwplot calling function. Dieter
Emmanuel Charpentier
2008-Aug-20 16:45 UTC
[R] Lattice: problem using panel.superpose and panel.groups
Le dimanche 17 ao?t 2008 ? 09:36 +0000, Dieter Menne a ?crit : [ Snip .. ]> Trellis graphics are a bit like hash functions: you can be close to the > target, but get a far-off result.Nice candidate for a fortune() entry ... Emmanuel Charpentier
Possibly Parallel Threads
- bwplot superpose panel.points from another dataframe
- Lattice: Superpose bwplot and dotplot [newbie question]
- Lattice: Superpose bwplot on dotplot [Newbie Question]
- superimposing different plot types in lattice panel.superpose
- superpose 2 time series with different time intervals