Ramnath
Ramnath

Reputation: 55735

Use Name of Data Frame Passed to Function as Plot Title

I have a function foo that takes a data frame as input and returns a ggplot object as output. I need to use the name of the data frame as the title of the plot. I am unable to figure out how to do this.

If I did not pass it to a function, I know that I could use deparse(substitute(df)) to get the desired title. But I am unable to do it inside the function.

Any thoughts on how to do this?

Upvotes: 1

Views: 1598

Answers (1)

Aniko
Aniko

Reputation: 18894

You have not given a minimal example to show the problem. The following works for me:

a <- expand.grid(x=1:3, y=1:2)
f <- function(df){qplot(x, y, data=a, main=deparse(substitute(df)))}
f(a)

Were you doing something else?

Upvotes: 7

Related Questions