ggplot is looking for thisData as a column of coffs. the most 'ggplotesque' way of doing this would be:
# melt your data to a "long" format: coffs.melt <- melt(coffs, id.vars = 'levels') # plot using colour aes parameter: ggplot(coffs.melt, aes(x=levels, y=value, colour=variable)) + geom_line() + ylab('Total Chargeoffs') this is untested since there is no sample data! Justin On Thu, Feb 16, 2012 at 2:50 PM, Keith Weintraub <kw1...@gmail.com> wrote: > Folks, > I want to automate some graphing using ggplot. > > Here is my code > graphChargeOffs2<-function(coffs) { > ggplot(coffs, aes(levels)) > dataNames<-names(coffs)[!names(coffs) == "levels"] > for(i in dataNames) { > thisData<-coffs[[i]] > last_plot() + geom_line(aes(y = thisData, colour = i)) > } > last_plot() + ylab("Total Chargeoffs") > } > > coffs is a data.frame. > > I get the following error: > Error in eval(expr, envir, enclos) : object 'thisData' not found > > As little as I know about environments in R I am pretty sure that the > geom_line in the loop is not able to see the thisData variable. > > Any help you could provide would be appreciated. I would be surprised if > there wasn't a way to pass the data into the geom_line function without > using environments. Of course I have been wrong once or twice in the past. > :) > > Note that geom_line also can't see the input variable "coffs". > > Thanks for any and all heo > > > > > -- > > > [[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. > [[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.