1

I have a function that has a parameter named "Legend". I'm trying to use this parameter in the mapping=aes(. statement in ggplot inside of the layer function. I've tried the following:

mapping=aes(x = carat, y = price, color = Legend),
mapping=aes(x = carat, y = price, aes_string(color = Legend)),
mapping=aes(x = carat, y = price, color = substitute(Legend))

All of these say "object 'Legend' not found" Is there any solution to this problem?

  • So `Legend` is a parameter defined inside a function (it's a local parameter), as in, it can only be seen by other things inside the same function. As (presumably) you're calling `ggplot` in the global environment, `Legend` essentially doesn't exist as it can't be seen globally. Though, posting your function which contains `Legend` would clarify the issue further. – Akhil Nair Sep 10 '15 at 14:44
  • This is a very subtle problem. See [this post](http://stackoverflow.com/questions/32391334/r-pass-graph-as-parameter-to-a-function/32393746#32393746) for an explanation and a solution. – jlhoward Sep 10 '15 at 15:15
  • 1
    BTW: Welcome to SO. Please read [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) for instructions on how to pose a good question. In brief you should *always* include a representative sample of your data, and code that runs and demonstrates the problem. – jlhoward Sep 10 '15 at 15:18
  • The "This is a very subtle problem." was very helpful but IMHO, this is a bug. – Philip Cannata Sep 10 '15 at 15:49
  • I fixed the problem by adding names(df)[names(df) == Legend] <- 'Legend' in my function – Philip Cannata Sep 10 '15 at 16:06

0 Answers0