OK, here is another approach.
 
Here is a function that will adjust the user coordinate system for you.
You give it 2 points in the current coordinates (x1 and y1) and the
values that you want the same 2 points to have in the new coordinate
system (x2 and y2) and it will change the coordinate system to match:

update.usr <- function(x1,y1=NULL,x2,y2=NULL) {
 xy1 <- xy.coords(x1,y1)
 xy2 <- if( missing(x2) && missing(y2) ) {
  xy.coords(y1)
 } else {
  xy.coords(x2,y2)
 }
 
 cur.usr <- par('usr')
 
 xslope <- diff(xy2$x)/diff(xy1$x)
 yslope <- diff(xy2$y)/diff(xy1$y)
 
 new.usr.x <- xslope * ( cur.usr[1:2] - xy1$x ) + xy2$x
 new.usr.y <- yslope * ( cur.usr[3:4] - xy1$y ) + xy2$y
 
 par(usr=c(new.usr.x, new.usr.y))
}
 
Here is an example of how to use it with your project:
 
> bp <- barplot(1:5)

x-coords of bar centers stored in 'bp'
 
> axis(1)
 
just to show what the current x-coordinates are
 

>  
> update.usr( bp[1:2], c(0,5), 1:2, c(0,20) )
 
adjust coordinate system so that bars 1 and 2 are centered at x vals of
1 and 2, and that the y range changes from 0-5 to 0-20.
 

> axis(3)
> axis(4)

show the new coordinate systems
 
 

> x <- 1:5
> y <- c(1,15,10,20,5)
>   
> lines(x,y, type='o', pch=16, cex=1.3)

add lines and points using the coordinates we want.
 
 
If your using 2 different y scales in the same plot, you should read
through the discussions on this list that have appeared recently
discussing the limitations/problems that can occur when using 2
different y scales in the same plot.
 
An improved version of this function will probably be included in a
future version of the TeachingDemos package (unless someone shows me
another existing function with the same capability, or Prof. Ripley
writes a better version to include in a core package).
 
 
Hope this helps,
 

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow@imail.org
(801) 408-8111



 


________________________________

	From: Anne-Katrin Link [mailto:anne.link@gmx.de] 
	Sent: Saturday, April 12, 2008 2:07 PM
	To: Greg Snow
	Cc: r-help@r-project.org
	Subject: Re: [R] two graphs in one figure?
	
	
	Dear Greg,
	
	thank you again for your help.
	
	The graph looks much better now. However, I need the bars to be
assigned to the left y-axis, and the lines should be assigned to the
right y-axis (the y-axes have different ranges,e.g. left y-axis 0-20,
and right y-axis 0-1). 
	Also would be nice if the bars had contour lines (more like the
barplot)... would that be possible?
	
	Thank you so much again.
	
	Anne-Katrin
	
------------------------------------------------------------------------
--------------------------------
	
	
	Barplot is probably not the best way here (it is possible, but
more work than it is worth).
	 
	Here is one approach that gives something similar to what you
want:
	 
	plot(x, y, lend=1, lwd=25, ylim=c(-1,10),
xlim=c(0.75,5.25),type='h', 
	 col='grey')
	lines(x, y2)
	 
	Is that good enough? or do you need the bars to look much more
like the barplot?

________________________________

	From: Anne-Katrin Link [mailto:anne.link@gmx.de]
	Sent: Sat 4/12/2008 7:11 AM
	To: Greg Snow
	Cc: r-help@r-project.org
	Subject: RE: [R] two graphs in one figure?
	
	
	Dear Greg, dear all,
	
	thank you for your reply!
	To clarify, here is an example of what I want to do (but better,
of course!):
	
	
	x<-     c(1,2,3,4,5)
	y<-     0:4
	y2<-   c(0,0,7,8,9)
	
	barplot(y ,ylim=c(-1,10), ylab="", xaxt="n",yaxt="n", main="")
	axis(4,at=c(1,2,3,4,5,6))
	text(6,2.9, "2nd y-axis", srt = 270, xpd = TRUE)
	par(new=TRUE)
	plot(x, y2, lty=2,type="l", xlab="x-axis", ylab= "y-axis")
	
	
	I dont think "barplot" is the right thing to use here since I
cant specify the x-values....what can I do to make this graph look
better? 
	
	Regards, 
	Anne-Katrin
	
	
	
	Using par(new=TRUE) can be tricky.
	 
	A better approach is to create one plot, then add the other
information to it.  You can add bars to an existing graph using barplot
with add=TRUE, you can add lines to an existing plot using the lines
function.
	 
	If you give more detail of what you want (examples of x, y, and
ynew) then we may be able to give more help.

________________________________

	From: r-help-bounces@r-project.org on behalf of Anne-Katrin Link
	Sent: Thu 4/10/2008 7:18 AM
	To: R Help
	Subject: [R] two graphs in one figure?
	
	

	Dear all,
	
	how can I plot a line graph and a bar graph in one single
figure? I tried
	to combine "barplot" and "plot". Even though they both have the
same
	x-values (1 to 55),  it just doesnt look as if they match in
their scale
	(the barplot is much wider than the "plot"....even though I
tried to put
	limits on the x-axis).
	Here is an example of what I did:
	
	barplot(y, xaxt="n",yaxt="n",ylim=c(-1,45), xlim=c(1,55))
	...
	par(new=TRUE)
	plot(x, ynew, lty=2, type="l", ylim=c(0,15), xlim=c(1,55))
	
	Another question: how can I make sure that the "0"-values from
the barchart
	are displayed as well?
	
	Thank you so much!
	
	Anne-Katrin
	   
	






	-- 
	GMX startet ShortView.de. Hier findest Du Leute mit Deinen
Interessen!
	Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx


	[[alternative HTML version deleted]]

