Hi, One problem is that you are printing the regression equation multiple times. Compare:
> system.time({ + p2 <- p1 + annotate(x = -0.1, y = 0.5, geom="text", label = lm_eqn(mydata), parse = TRUE) + print(p2) + }) user system elapsed 0.930 0.006 0.976 > system.time({ + p3 <- p1 + geom_text(aes(x = -0.1, y = 0.5, label = lm_eqn(mydata)), parse = TRUE) + print(p3) + }) user system elapsed 38.667 0.046 38.956 So, use annotate instead of geom_text (as a bonus the equation will look better). Further speedups are possible, but this is probably the biggest problem. Best, Ista On Mon, Jan 21, 2013 at 4:21 PM, Janesh Devkota <janesh.devk...@gmail.com> wrote: > Hello All, > > I have plotted a scatter plot in ggplot2 and added a regression line and a > regression equation. But the processing is very very slow. One reason might > be because I have so many data pairs. Is there any way to speed up this > code ? I need to create a multiple layout as well. > > The code I have used is as follows: > > setwd("C:/Users/jzd0009/Documents/R software") > mydata <- read.table("dataset.csv",header=TRUE,sep=",") > library(ggplot2) > p <- > ggplot(mydata,aes(date))+geom_line(aes(y=modeled,colour="modeled"))+geom_line(aes(y=observed,colour="observed")) > p > > > p1 <- ggplot(mydata, aes(modeled,observed))+geom_point(aes(y=observed)) > #p1 <- p1+stat_smooth() > > lm_eqn = function(mydata){ > m = lm(modeled ~ observed, mydata); > eq <- substitute(italic(y) == a + b %.% > italic(x)*","~~italic(r)^2~"="~r2, > list(a = format(coef(m)[1], digits = 2), > b = format(coef(m)[2], digits = 2), > r2 = format(summary(m)$r.squared, digits = 3))) > as.character(as.expression(eq)); > } > p1 <- p1 + geom_text(aes(x = -0.1, y = 0.5, label = lm_eqn(mydata)), parse > = TRUE) > p1 <- p1+geom_smooth(method="lm",se=FALSE,color="green",formula=y~x,lwd=2) > p1 > > #For multiple layout > > library(grid) > grid.newpage() > pushViewport(viewport(layout=grid.layout(2,2))) > vplayout <- function(x,y) > viewport(layout.pos.row=x,layout.pos.col=y) > print(p,vp=vplayout(1,1)) > print(p1,vp=vplayout(1,2)) > print(p,vp=vplayout(2,1)) > print(p1,vp=vplayout(2,2)) > > > The data for the above code can be found on > https://www.dropbox.com/s/1xrgvnge0prf0a6/dataset.csv > > Thank you so much. > > Best Regards, > Janesh Devkota > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.