将参数传递给包装器中的ggplot [英] Passing arguments to ggplot in a wrapper

查看:117
本文介绍了将参数传递给包装器中的ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将ggplot2封装到另一个函数中,并希望能够以与接受它们相同的方式解析变量,是否有人能够让我朝着正确的方向前进。



例如,我们考虑下面的MWE。

  #Load必需的库。 
库(ggplot2)

##我的包装函数。
mywrapper< - function(data,xcol,ycol,colorVar){
writeLines(This is my wrapper)
plot < - ggplot(data = data,aes(x = xcol,y = ycol,color = colorVar))+ geom_point()
print(plot)
return(plot)
}

虚拟数据

 # #Demo Data 
myData< - data.frame(x = 0,y = 0,c =Color Series)

现有的用法无需执行:

  ##示例原始函数用法,其按原样执行
plot <-ggplot(data = myData,aes(x = x,y = y,color = c))+ geom_point()
print(plot)

目标使用语法:

  ##引发错误的预期用法示例-----object'xcol'not found
mywrapper(data = myData,xcol = x ,ycol = y,colorVar = c)

以上举例说明了原始 ggp lot2软件包,以及我想如何将它包装在另一个函数中。然而,这个包装会抛出一个错误。

我相信这适用于很多其他应用程序,并且它可能已经被回答了一千次,但是,我不确定是什么这个主题是在R内调用的。

解决方案

这里的问题是ggplot寻找一个在数据对象中命名为 xcol 。我建议切换到使用 aes_string 并传递要使用字符串映射的列名称,例如:

  mywrapper(data =myData,xcol =x,ycol =y,colorVar =c)

然后修改你的包装:

  mywrapper<  - 函数(data,xcol,ycol,colorVar){
writeLines(This is my wrapper)
plot < - ggplot(data = data,aes_string(x = xcol,y = ycol,color = colorVar ))+ geom_point()
print(plot)
return(plot)
}

一些言论:


  1. 个人喜好,我在eg周围使用了很多空格 x = 1 ,对我来说,这大大提高了可读性。如果没有空格,代码看起来像一个大块。

  2. 如果您将绘图返回到函数外部,我不会将其打印到函数内部,而是在函数外部打印。


I need to wrap ggplot2 into another function, and want to be able to parse variables in the same manner that they are accepted, can someone steer me in the correct direction.

Lets say for example, we consider the below MWE.

#Load Required libraries.
library(ggplot2)

##My Wrapper Function.
mywrapper <- function(data,xcol,ycol,colorVar){
  writeLines("This is my wrapper")
  plot <- ggplot(data=data,aes(x=xcol,y=ycol,color=colorVar)) + geom_point()
  print(plot)
  return(plot)
}

Dummy Data:

##Demo Data
myData <- data.frame(x=0,y=0,c="Color Series")

Existing Usage which executes without hassle:

##Example of Original Function Usage, which executes as expected
plot <- ggplot(data=myData,aes(x=x,y=y,color=c)) + geom_point()
print(plot)

Objective usage syntax:

##Example of Intended Usage, which Throws Error ----- "object 'xcol' not found"
mywrapper(data=myData,xcol=x,ycol=y,colorVar=c)

The above gives an example of the 'original' usage by the ggplot2 package, and, how I would like to wrap it up in another function. The wrapper however, throws an error.

I am sure this applies to many other applications, and it has probably been answered a thousand times, however, I am not sure what this subject is 'called' within R.

解决方案

The problem here is that ggplot looks for a column named xcol in the data object. I would recommend to switch to using aes_string and passing the column names you want to map using a string, e.g.:

mywrapper(data = "myData", xcol = "x", ycol = "y", colorVar = "c")

And modify your wrapper accordingly:

mywrapper <- function(data,xcol,ycol,colorVar){
  writeLines("This is my wrapper")
  plot <- ggplot(data=data, aes_string(x = xcol, y = ycol, color = colorVar)) + geom_point()
  print(plot)
  return(plot)
}

Some remarks:

  1. Personal preference, I use a lot of spaces around e.g. x = 1, for me this greatly improves the readability. Without spaces the code looks like a big block.
  2. If you return the plot to outside the function, I would not print it inside the function, but just outside the function.

这篇关于将参数传递给包装器中的ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆