John Sorkin
2008-Nov-22 02:09 UTC
[R] Overlay two plots on the same device with axes having the same scale.
R 2.6.0
Windows XP
I have crated a file containing data with x and y values and have plotted the
data using the output of gam. I would like to overlay an x,y plot of the data on
top of the line returned by gam. I have succeeded in doing this using
par(new=TRUE), unfortunately the y axis of the overlayed plots are of slightly
different scale. How can I set the axes of the overlay plots so the scales are
exactly the same?
y<-matrix(nrow=200,ncol=2)
# Create data
y[1:200,2]<-sin((0:99)/(0.5*10*pi))+rnorm(100,0,.2)
y[,1]<-1:200
# Plot of raw data.
plot(y[,1],y[,2])
oldpar<-par(new=TRUE)
fit1<-gam(y[,2]~s(y[,1]))
#Plot of fit - overlays plot of raw data.
#Note scale of overaly is not the same as the original raw data plot.
summary(fit1)
plot(fit1)
Thanks,
John
John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
Confidentiality Statement:
This email message, including any attachments, is for th...{{dropped:6}}
Ben Bolker
2008-Nov-24 03:03 UTC
[R] Overlay two plots on the same device with axes having the same scale.
John Sorkin <jsorkin <at> grecc.umaryland.edu> writes:> I have crated a file containing data with x and y > values and have plotted the data using the output of gam. I > would like to overlay an x,y plot of the data on top of > the line returned by gam.Why not plot the gam fit first and then use points(x,y) to add the original data to the plot? Ben Bolker