Trouble with bargraph in figure mixing basic and trellis plots

#1
I am creating a multi-plot figure with a scatter plot at the top of the page and a bar chart on the lower part. The bar chart shows the frequecies of Types 1, 2 and 3 at various times. Each bar is of height = 1, subsections of bars proportional to the frequency, and each bar located at the correct (time) position on a continuous X-axis.

I can make each plot work individually, but the only solution I found for the bar plot involves lattice plots, and I can't figure out how to put both the basic plot and the (lattice) bar plot on the same page. I have boiled my problem down to this minimal example:
Code:
# Example data 
tC <- textConnection(" 
Time Type1 Type2 Type3 
1.3 .50 .25 .25 
4.5 .55 .25 .20 
5.2 .65 .20 .15 
") 

data1 <- read.table(header=TRUE, tC) 
data2 <- data.frame(Time=rep(data1$Time, 3), stack(data1[,2:4])) 
close.connection(tC) 

#PLOT1 Scatterplot 
par(mfrow=c(2,1)) 
plot(data1$Time, data1$Type1, frame=T, ylab="Divergence", col=rgb(0,100,0,50,maxColorValue=255), main="plot 1", xlim= c(0,6), ylim= c(0, 1), axes=FALSE, xlab=" ") 

#PLOT2 barplot 
require(lattice) 
plot2 <- xyplot(values ~ Time, group=ind, data=data2, stack=TRUE, horizontal=FALSE, panel=panel.barchart, ylim=c(-0.05,1.05), xlim=c(0,6), main="Plot 2- how to plot on same page below plot1?") 
print(plot2)
The only thing left is to get both plots to be vertically aligned, one above the other on the same figure. Is this possible? Thanks,
Marcel
 
Last edited by a moderator:

Dason

Ambassador to the humans
#2
It's definitely possible and the following should do about what you want but I'm sure there's a better way to do it. The "newpage" and "split" options in the print.trellis statement are what's important
Code:
require(lattice)
?print.trellis

par(mfrow=c(2,1)) 

#PLOT1 Scatterplot 
plot(data1$Time,
	data1$Type1,
	frame=T, 
	ylab="Divergence", 
	col=rgb(0,100,0,50,maxColorValue=255), 
	main="plot 1", 
	xlim= c(0,6), 
	ylim= c(0, 1), 
	axes=FALSE, 
	xlab=" ")

#PLOT2 barplot 
plot2 <- xyplot(values ~ Time,
	group=ind,
	data=data2, 
	stack=TRUE, 
	horizontal=FALSE, 
	panel=panel.barchart, 
	ylim=c(-0.05,1.05), 
	xlim=c(0,6), 
	main="Plot 2- how to plot on same page below plot1?") 

print(plot2, split = c(1,2,1,2), newpage = FALSE)