开发者

Use Name of Data Frame Passed to Function as Plot Title

开发者 https://www.devze.com 2023-01-29 13:42 出处:网络
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 thi

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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消