Thomas Schönhoff
2005-Apr-21 10:59 UTC
[R] Howto overlay two plots and save them in one pdf file?
Hello, I do have two different plots from LAD and OLS regression objects like this: # LAD regression and related plot rq(formula = SBP ~ Age) f = coef(rq(SBP ~ Age)) pred = f[1] + f[2]*Age plot (Age, SBP) lines (Age, pred) # OLS regression and related plot Pred = lm(SBP ~ Age) plot (Age, SBP) lines (Age,fitted(Pred)) Well for comparatative reason I would would like to subsume both plots into a unifying plot and save them in one file.pdf. I tried to find an answer in FAQ and mailinglists archive, no luck. Maybe did miss an appropiate answer to my question, so a pointer to solve my problem woud be sufficient! Much thanks Thomas My system: platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 2 minor 0.1 year 2004 month 11 day 15 language R
Andy Bunn
2005-Apr-21 12:12 UTC
[R] Howto overlay two plots and save them in one pdf file?
> Well for comparatative reason I would would like to subsume both plots > into a unifying plot and save them in one file.pdf. > I tried to find an answer in FAQ and mailinglists archive, no luck. > Maybe did miss an appropiate answer to my question, so a pointer to > solve my problem woud be sufficient!Do you mean a second y axis? If so then something like this would do it: library(quantreg) Age <- rnorm(50) SBP <- Age * runif(50) # LAD regression and related plot rq(formula = SBP ~ Age) f = coef(rq(SBP ~ Age)) pred = f[1] + f[2]*Age # leave room for the second axis pdf("junk.pdf") par(mar = c(3,3,1,3), mgp = c(2, 1, 0)) plot (Age, SBP, ylab = "SBP: QR Fit") lines (Age, pred) par(new=T) # OLS regression and related plot Pred = lm(SBP ~ Age) plot (Age, SBP, col = "blue", ylab = "", xlab = "", axes = F, pch = "+") lines (Age,fitted(Pred), col = "blue") axis(4, at=pretty(range(SBP))) mtext("SBP: OLS Fit", 4, 2, col = "blue") dev.off() HTH, Andy
Andy Bunn
2005-Apr-21 17:29 UTC
[R] Howto overlay two plots and save them in one pdf file?
> > Do you mean a second y axis? If so then something like this would > > do it > > Not exactly, I would also take advantage of overlaying the fitting > curve of LAD on OLS, since both rely on the same dataset. Maybe two > regression lines with varying shapes (i.E. straight versus dotted > line)Like this? library(quantreg) Age <- rnorm(50) SBP <- Age * runif(50) pdf("junk.pdf") # plot the data plot (Age, SBP, ylab = "SBP") # LAD regression and related line abline(rq(formula = SBP ~ Age), col = "red") # OLS regression and related line abline(lm(SBP ~ Age), col = "blue", lty = "dotted") # make a legend legend(-2,1, c("LAD", "LM"), col = c("red", "blue"), text.col= c("red", "blue"), lty = c("solid", "dotted")) dev.off() HTH, Andy
Seemingly Similar Threads
- rob var/cov + LAD regression
- Locate Patients who have multiple high blood pressure readings
- [PATCH] Updated udp.c to use real client ip and subnetmask values if on local subnet
- "get" problem
- [PATCH] Updated udp.c to use real client ip and subnetmask values if on local subnet