genghis
2011-Jul-04 01:52 UTC
[R] superimposing different plot types in lattice panel.superpose
I would like to plot 3 best-fit models in a single panel of a lattice plot, superimposed on 3 corresponding datasets in the same panel. My goal is to show the models as lines of 3 different colors, and the data as points whose colors correspond to the model colors. In essence, I have two levels of grouping: 1) model vs. data, and 2) model number. Since there is only one ?groups? variable, I have tried to deal with the additional grouping level by subsetting the data inside a custom panel function (basically a hack), but something about the way parameters are passed in panel.superpose (I think) is making it hard to show both points and lines. My question is very similar to a previous post: http://www.ask.com/web?q=r%20panel.superpose%20bwplot%20sim%20actual&o=15527&l=dis&prt=NIS&chn=retail&geo=US&ver=18 , but the questioner in that case was using bwplot, which automatically makes a separate plot for every level of the categorical variable, so they didn?t face the two-level grouping problem, and I have been unable to figure out how to adapt their answer. Another approach I tried was to put my model function inside of my custom panel function so that the analysis occurred there, but I couldn?t get it to subset the x and y data appropriately. In the toy problem below I want to plot each inverted V (?model?) as LINES, with a single POINT (?data?) in the center of each inverted V, the same color as the inverted V. The code runs, but I can?t seem to mix lines and points. In my real problem (stable isotopes with ellipses superimposed on data) there will be additional panels but I am creating only one panel here, for simplicity. #generate test "model results" x<-1:9 y<-rep(c(1,3,1),3) model<-c(rep("a",3),rep("b",3),rep("c",3)) modelresults<-data.frame(x,y,model) #generate test "data" x<-c(2,5,8) y<-rep(1.5,3) model<-c("a","b","c") data<-data.frame(x,y,model) #combine them into one data set combined<-make.groups(modelresults,data) #custom panel function panel.dualplot <- function(...) { panel.xyplot(x[which="modelresults"],y[which="modelresults"],...) panel.points(x[which="data"],y[which="data"],...) } #main call to xyplot xyplot(y ~ x, data=combined, type = "l", panel = panel.superpose, groups = model, panel.groups = panel.dualplot, ) I?d be very grateful for any suggestions. Thanks! John -- View this message in context: http://r.789695.n4.nabble.com/superimposing-different-plot-types-in-lattice-panel-superpose-tp3642808p3642808.html Sent from the R help mailing list archive at Nabble.com.
Dennis Murphy
2011-Jul-04 07:00 UTC
[R] superimposing different plot types in lattice panel.superpose
Hi: Here's one way out, if I read your intention properly... plot1 <- xyplot(y ~ x, data = modelresults, groups = model, type = 'l', xlim = c(0, 10), ylim = c(0.5, 3.5)) plot2 <- xyplot(y ~ x, data = data, groups = model, type = 'p', pch = 16, xlim = c(0, 10), ylim = c(0.5, 3.5)) plot1 + plot2 You might need latticeExtra to combine the plots (??); I had it loaded with lattice when I did the plot. HTH, Dennis On Sun, Jul 3, 2011 at 6:52 PM, genghis <jcpayne at uw.edu> wrote:> I would like to plot 3 best-fit models in a single panel of a lattice plot, > superimposed on 3 corresponding datasets in the same panel. ?My goal is to > show the models as lines of 3 different colors, and the data as points whose > colors correspond to the model colors. ?In essence, I have two levels of > grouping: 1) model vs. data, and 2) model number. ?Since there is only one > ?groups? variable, I have tried to deal with the additional grouping level > by subsetting the data inside a custom panel function (basically a hack), > but something about the way parameters are passed in panel.superpose (I > think) is making it hard to show both points and lines. > > My question is very similar to a previous post: > http://www.ask.com/web?q=r%20panel.superpose%20bwplot%20sim%20actual&o=15527&l=dis&prt=NIS&chn=retail&geo=US&ver=18 > , but the questioner in that case was using bwplot, which automatically > makes a separate plot for every level of the categorical variable, so they > didn?t face the two-level grouping problem, and I have been unable to figure > out how to adapt their answer. ?Another approach I tried was to put my model > function inside of my custom panel function so that the analysis occurred > there, but I couldn?t get it to subset the x and y data appropriately. > > In the toy problem below I want to plot each inverted V (?model?) as LINES, > with a single POINT (?data?) in the center of each inverted V, the same > color as the inverted V. ?The code runs, but I can?t seem to mix lines and > points. ?In my real problem (stable isotopes with ellipses superimposed on > data) there will be additional panels but I am creating only one panel here, > for simplicity. > > #generate test "model results" > x<-1:9 > y<-rep(c(1,3,1),3) > model<-c(rep("a",3),rep("b",3),rep("c",3)) > modelresults<-data.frame(x,y,model) > > #generate test "data" > x<-c(2,5,8) > y<-rep(1.5,3) > model<-c("a","b","c") > data<-data.frame(x,y,model) > > #combine them into one data set > combined<-make.groups(modelresults,data) > > #custom panel function > panel.dualplot <- function(...) { > ? ?panel.xyplot(x[which="modelresults"],y[which="modelresults"],...) > ? ?panel.points(x[which="data"],y[which="data"],...) > } > > #main call to xyplot > xyplot(y ~ x, data=combined, > type = "l", > panel = panel.superpose, > groups = model, > panel.groups = panel.dualplot, > ) > > > I?d be very grateful for any suggestions. > > Thanks! > > John > > > > -- > View this message in context: http://r.789695.n4.nabble.com/superimposing-different-plot-types-in-lattice-panel-superpose-tp3642808p3642808.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >