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]]
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 <kw1958@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]]
A kind respondent mentioned that I gave no data for this data set. Here is a subset in CSV format: "","levels","m101","m101111" "1",0.15,0.00166736248903737,0.00525782746279257 "2",0.16,0.000671421429082792,0.00263263808833129 "3",0.17,0.000248914508386666,0.00125478603676572 "4",0.18,8.62700534705944e-05,0.000575729980347196 "5",0.19,2.71841670884316e-05,0.000251684211328049 Here is the CSV file. I hope it's OK to send it to the mailing list. Thanks again, KW -- On Feb 16, 2012, at 5:50 PM, Keith Weintraub 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 > > > > > -- >