[R] Problem building up ggplot graph in a loop.

Keith Weintraub kw1958 at gmail.com
Fri Feb 17 01:05:59 CET 2012


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
> 
> 
> 
> 
> --
> 



More information about the R-help mailing list