Skip to content

Plot command overwrites existing plot in multiple figure plot

3 messages · marcel, David Winsemius

#
Hi all,
I have a figure with 5 separate graphs in a stacked layout. While most of my
plots drop into their appointed spots nicely, my last plot, using lattice
and the plot(object) command insists on wiping out my current plot window
altogether, and just plotting the last figure by itself. The relevant part
of my code is of the form:

nf <-
layout(matrix(c(1,1,1,1,0,0,2,2,2,2,0,0,3,3,3,3,0,0,4,4,4,4,0,0,5,5,5,5,0,0),5,6,
byrow=TRUE), respect=FALSE)

#first plot, plots well
attach(data1)
par(mar=c(0.2,4.5,0.2,0.5))
plot(var1, var2, frame=T, etc..) 
detach(data1)

#second plot, plots fine
attach(data2)
par(mar=c(0.2,4.5,0.2,0.5))
plot(var3, var4, frame=T, etc...) 
detach(data1)

require(lattice)
tmpdf <- data.frame(Mydata)
tmpdf                               #data show up fine
barchart(values ~ Time, group=ind, data=tmpdf, stack=TRUE, horizontal=FALSE)
xyplot(values ~ Time, group=ind, data=tmpdf, stack=TRUE, horizontal=FALSE,
panel=panel.barchart, ylim=c(-0.05,1.05), xlim=c(0,6))

# Problem here. This command will wipe out my existing window (prints
briefly and is then quickly replaced by the plot below
print(xyplot(values ~ Time, group=ind, data=tmpdf, stack=TRUE,
horizontal=FALSE, panel=panel.barchart, ylim=c(-0.05,1.05), xlim=c(0,6)))

Is there any way around this problem?

Marcel


--
View this message in context: http://r.789695.n4.nabble.com/Plot-command-overwrites-existing-plot-in-multiple-figure-plot-tp3661245p3661245.html
Sent from the R help mailing list archive at Nabble.com.
#
On Jul 11, 2011, at 9:24 PM, marcel wrote:

            
... on a google search, multiple links to::: r-project mixing grid and  
base graphics

www.stat.ucl.ac.be/ISdidactique/Rhelp/library/gridBase/doc/gridBase.pdf
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
1 day later
#
This problem has been an opportunity for me to learn about issues mixing base
graphics with lattice plots. From the many pdfs and blogs and other things I
have found, it seems that I have two options: 1) insert a viewport of my bar
graph into a basic environment; or 2) do all of my plots in lattice. 

I have tried each and have encountered difficulties. I think the simplest
approach should be to add a lattice bar graph to the rest of my plot,
because all of the plots are already made. Unfortunately, despite the many
references to these issues, I was not able to find an example of this being
done. I think it should involve 

I have most of the parts working, but am not sure how to put the last pieces
together: 


nf <-
layout(matrix(c(1,1,1,1,0,0,2,2,2,2,0,0,3,3,3,3,0,0,4,4,4,4,0,0,5,5,5,5,0,0),5,6,
byrow=TRUE), respect=FALSE)
 
# first my regular plots
attach(mydatafile1)
par(mar=c(0.2,4.5,0.2,0.5))
plot(xvalue, yvalue, frame=T)
detach(mydatafile1)

attach(mydatafile2)
par(mar=c(0.2,4.5,0.2,0.5))
plot(xvalue, yvalue, frame=T)
detach(mydatafile2)

# now the bar plot in lattice, which I know will plot fine in its own window
require(lattice)
tmpdf <- data.frame(Time=rep(tmp$Time, 3), stack(tmp[,2:4]))
barchart(values ~ Time, group=ind, data=tmpdf, stack=TRUE, horizontal=FALSE)
plot5 <- xyplot(values ~ Time, group=ind, data=tmpdf, stack=TRUE,
horizontal=FALSE, panel=panel.barchart)

# here is the issue, how to get this into basic. I think I need a
pushViewport command of some kind
lviewport(x=unit(1, "npc") - unit(1, "inches"), y=0.5, width=0.2,
height=0.5, just=c("right", "centre")))
print(plot3, position=c(0.01, 0, 1, 0.5))

Any thoughts about how to put the last pieces together would be very much
appreciated. 

Marcel

--
View this message in context: http://r.789695.n4.nabble.com/Plot-command-overwrites-existing-plot-in-multiple-figure-plot-tp3661245p3665545.html
Sent from the R help mailing list archive at Nabble.com.