Dear List, I have a wrapper function that draws a graph that I'd like to use in a vector-like manner. The for-loop version I currently use is below. library(ggplot2) data(economics) h <- 600 w <- 800 #---------------------------------------------------------- draw_metric_by_date <- function( df, i, smooth=FALSE, BASEPATH ) { mlabel <- names(df)[i] qmetric <- qplot( data=df, x=date, y=df[,i], geom=c('line','jitter'), ylab=mlabel, main=mlabel, colour=I("dark blue") ) if ( smooth == TRUE ) { print("smoother requested...") tmp <- qmetric qmetric <- tmp + stat_smooth() } pngfn <- paste( BASEPATH, mlabel, ".png", sep="") print(pngfn) png( file=pngfn, height=h, width=w ) print(qmetric) dev.off() } basepath <- "C:/tmp" for ( i in c(2:6) ) { print(names(economics)[i]) draw_metric_by_date( economics, i, smooth=TRUE, basepath ) } Could someone show me how to do the same with a vector approach with an "apply" function ? Many thanks, Avram
Hi, Here's one approach that I find perhaps more elegant than sweeping through the columns by their index,> library(ggplot2) > data(economics) > > str(economics) > > # library(reshape) > d <- melt(economics, id="date") > > > foo <- function(var="pop", d, smooth=FALSE, ... ) { > p <- qplot( data=subset(d, variable==var), x=date, y=value, > geom=c('line','jitter'), > ylab=var, main=var, colour=I("dark blue") ) > > if ( smooth == TRUE ) { > print("smoother requested...") > return(print(p + stat_smooth() ) ) > } > > return(print(p)) > } > > foo(d=d) > > > pdf() > # library(plyr) > l_ply(levels(d$variable), foo, d=d, smooth=T) > # or lapply() > dev.off()HTH, baptiste On 29 Apr 2009, at 20:59, Avram Aelony wrote:> > Dear List, > > I have a wrapper function that draws a graph that I'd like to use in > a vector-like manner. The for-loop version I currently use is below. > > library(ggplot2) > data(economics) > h <- 600 > w <- 800 > > #---------------------------------------------------------- > draw_metric_by_date <- function( df, i, smooth=FALSE, BASEPATH ) { > mlabel <- names(df)[i] > qmetric <- qplot( data=df, x=date, y=df[,i], > geom=c('line','jitter'), ylab=mlabel, main=mlabel, colour=I("dark > blue") ) > > if ( smooth == TRUE ) { > print("smoother requested...") > tmp <- qmetric > qmetric <- tmp + stat_smooth() > } > > > pngfn <- paste( BASEPATH, mlabel, ".png", sep="") > print(pngfn) > png( file=pngfn, height=h, width=w ) > print(qmetric) > dev.off() > } > > > basepath <- "C:/tmp" > for ( i in c(2:6) ) { > print(names(economics)[i]) > draw_metric_by_date( economics, i, smooth=TRUE, basepath ) > } > > Could someone show me how to do the same with a vector approach with > an "apply" function ? > > Many thanks, > Avram > > ______________________________________________ > R-help at 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._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
Dear List, Hadley offered the following solution:>library(plyr) >l_ply(2:6, draw_metric_by_date, df = economics, smooth = TRUE, BASEPATH = basepath)Many thanks, Avram On Wednesday, April 29, 2009, at 12:59PM, "Avram Aelony" <aavram at mac.com> wrote:> >Dear List, > >I have a wrapper function that draws a graph that I'd like to use in a vector-like manner. The for-loop version I currently use is below. > >library(ggplot2) >data(economics) >h <- 600 >w <- 800 > >#---------------------------------------------------------- >draw_metric_by_date <- function( df, i, smooth=FALSE, BASEPATH ) { > mlabel <- names(df)[i] > qmetric <- qplot( data=df, x=date, y=df[,i], geom=c('line','jitter'), ylab=mlabel, main=mlabel, colour=I("dark blue") ) > > if ( smooth == TRUE ) { > print("smoother requested...") > tmp <- qmetric > qmetric <- tmp + stat_smooth() > } > > > pngfn <- paste( BASEPATH, mlabel, ".png", sep="") > print(pngfn) > png( file=pngfn, height=h, width=w ) > print(qmetric) > dev.off() >} > > >basepath <- "C:/tmp" >for ( i in c(2:6) ) { > print(names(economics)[i]) > draw_metric_by_date( economics, i, smooth=TRUE, basepath ) >} > >Could someone show me how to do the same with a vector approach with an "apply" function ? > >Many thanks, >Avram > >______________________________________________ >R-help at 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. > >