将参数传递给ggplot

发布于 2024-11-17 10:18:56 字数 446 浏览 1 评论 0原文

想要创建一个使用 ggplot 生成图形的函数。为了简单起见,典型的图形可能是“

ggplot(car, aes(x=speed, y=dist)) + geom_point() 

我想创建的函数是”类型

f <- function(DS, x, y) ggplot(DS, aes(x=x, y=y)) + geom_point()

,但是“这”不起作用,因为 x 和 y 不是字符串。这个问题已经在之前的SO问题中注意到(例如,这个),但在我看来,没有提供令人满意的答案。如何修改上述函数以使其适用于任意数据帧?

would like to create a function that generates graphs using ggplot. For the sake of simplicity, the typical graph may be

ggplot(car, aes(x=speed, y=dist)) + geom_point() 

The function I would like to create is of the type

f <- function(DS, x, y) ggplot(DS, aes(x=x, y=y)) + geom_point()

This however won't work, since x and y are not strings. This problem has been noted in previous SO questions (e.g., this one), but without providing, in my view, a satisfactory answer. How would one modify the function above to make it work with arbitrary data frames?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少钕鈤記 2024-11-24 10:18:56

一种解决方案是将 x 和 y 作为数据框 DS 中列的字符串名称传递。

f <- function(DS, x, y) {    
  ggplot(DS, aes_string(x = x, y = y)) + geom_point()  
}

然后将该函数调用为:

f(cars, "speed", "dist")

但是,您似乎不希望这样?您能举例说明为什么您需要不同的功能吗?是因为您不想将参数放在同一个数据框中吗?

One solution would be to pass x and y as string names of columns in data frame DS.

f <- function(DS, x, y) {    
  ggplot(DS, aes_string(x = x, y = y)) + geom_point()  
}

And then call the function as:

f(cars, "speed", "dist")

However, it seems that you don't want that? Can you provide an example why you would need different functionality? Is it because you don't want to have the arguments in the same data frame?

她如夕阳 2024-11-24 10:18:56

我认为可能有以下类型的代码,它们仅构建 aes 组件。

require(ggplot2)

DS <- data.frame(speed=rnorm(10), dist=rnorm(10))

f <- function(DS, x, y, geom, opts=NULL) {
  aes <- eval(substitute(aes(x, y),
    list(x = substitute(x), y = substitute(y))))
  p <- ggplot(DS, aes) + geom + opts
}

p <- f(DS, speed, dist, geom_point())
p

然而,这似乎是一个复杂的方法。

I think it's possible the following type of codes, which only build the aes component.

require(ggplot2)

DS <- data.frame(speed=rnorm(10), dist=rnorm(10))

f <- function(DS, x, y, geom, opts=NULL) {
  aes <- eval(substitute(aes(x, y),
    list(x = substitute(x), y = substitute(y))))
  p <- ggplot(DS, aes) + geom + opts
}

p <- f(DS, speed, dist, geom_point())
p

However, it seems to be complicated approach.

美煞众生 2024-11-24 10:18:56

另一种选择是使用 do.call。这是来自工作代码的一行复制粘贴:

gg <- gg + geom_rect( do.call(aes, args=list(xmin=xValues-0.5, xmax=xValues+0.5, ymin=yValues, ymax=rep(Inf, length(yValues))) ), alpha=0.2, fill=colors )

Another option is to use do.call. Here is a one line copy paste from a working code:

gg <- gg + geom_rect( do.call(aes, args=list(xmin=xValues-0.5, xmax=xValues+0.5, ymin=yValues, ymax=rep(Inf, length(yValues))) ), alpha=0.2, fill=colors )
别闹i 2024-11-24 10:18:56

我能想到的一种方法是使用 match.call() 来访问传递给自定义绘图函数的参数/参数所包含的变量名称,然后使用 eval() > 关于他们。通过这种方式,如果您不喜欢的话,您可以避免将它们作为引用传递给您的自定义函数。

library(ggplot2)

fun <- function(df, x, y) {
    arg <- match.call()
    ggplot(df, aes(x = eval(arg$x), y = eval(arg$y))) + geom_point()
} 
fun(mpg, cty, hwy) # no need to pass the variables (column names) as quoted / as strings

输入图片此处描述

One approach that I can think of is using match.call() to reach the variable names contained by the parameters/arguments passed to the custom plotting function and then use eval() on them. In this way you avoid passing them as quoted to your custom function, if you do not like that.

library(ggplot2)

fun <- function(df, x, y) {
    arg <- match.call()
    ggplot(df, aes(x = eval(arg$x), y = eval(arg$y))) + geom_point()
} 
fun(mpg, cty, hwy) # no need to pass the variables (column names) as quoted / as strings

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文