从自定义函数实现GEOM选项 [英] Implementing geom options from a custom function

查看:193
本文介绍了从自定义函数实现GEOM选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我倾向于用大量的定制的令人难以置信的复杂 ggplot 人物结束了,所以我很自然地,这些对我code添加为函数,这样我可以很容易地重用他们。当我想用我的自定义功能中的一个出现该问题,但稍微调整它,例如删除或添加的审美,我已经在函数中定义一个GEOM。我的这个选项是:

I tend to end up with incredibly complicated ggplot figures with a lot of customization, so I naturally add these to my code as functions so that I can easily reuse them. The problem occurs when I want to use one of my custom functions, but tweaking it slightly, e.g. removing or adding an aesthetic to a geom that I already define inside the function. My options for this are:


  1. 创建一个几乎重复的功能,但与特定的变化增加,并改变了函数名,以反映这种或

  1. Create an almost duplicate function but with the particular change added, and the function name changed to reflect this or

参数添加到该改变功能如何使用 ggplot 在函数内部构造

Add arguments to the function that change how the ggplot is constructed inside the function

我尝试尽可能多的选择2.因为我可以,因为它清楚地减少了我的剧本的冗余和混乱的金额。

I try and opt for 2. as much as I can because it clearly reduces the amount of redundancy and clutter in my scripts.

不过有的时候2.迫使我重新输入含有几个参数整GEOM功能几乎退化为1.函数内。我举个很简单的例子,记住霸菱,我已经远远复杂的功能和价值,我在这里做了深思:

However there are times when 2. almost degenerates to 1. inside the function by forcing me to retype whole geom functions containing several arguments. I will give a very simple example, baring in mind that the functions I have are far complicating and worth the pondering that I am doing here:

gg_custom_point <- function(df, xvar, yvar, gvar=NA){
    g <- ggplot(df)

    if(is.na(gvar)){
        # do geom without colour
        g <- g + geom_point(aes_string(x=xvar, y=yvar))
    }
    else{
        # do geom with colour - mostly redundant code
        g <- g + geom_point(aes_string(x=xvar, y=yvar, colour=gvar))
    }
    return(g)
}

# I can use the same function to make slightly different custom plots
gg_custom_point(mtcars, "wt", "mpg")
gg_custom_point(mtcars, "wt", "mpg", "qsec")

问题是,我不得不重新输入整个 geom_point ,而不是能只是增加了美感。我必须键入了包含美学可能的形式给定参数的每个组合的GEOM - 如果我希望能够调整多种美学一GEOM冗余问题更是雪上加霜。

The issue is that I have had to retype the whole geom_point instead of being able to just add the aesthetic. The redundancy problem is even worse if I ever want to be able to tweak multiple aesthetics for one geom - I have to type out the geom containing every combination of aesthetics possible form the given arguments.

我可以这样做:

g <- g + geom_point(aes_string(x=xvar, y=yvar))
if(!is.na(gvar)){
    # Add to the global aesthetic - no need to retype the whole geom
    g <- g + aes_string(colour=gvar)
}

但是,这并不使用多个数据源扩展到多个geoms,因为所有geoms需要使用这种颜色的审美和 GVAR 列。

有容易增加美感到已经被添加到 ggplot 对象特定geoms的方法吗?另外,有使用自定义函数中 GGPLOT2 功能和geoms一个更好的办法?

Is there a way of easily adding aesthetics to specific geoms that have already been added to the ggplot object? Alternatively, is there a better way of using ggplot2 functions and geoms inside a custom function?

推荐答案

我不知道这是你需要什么比较 - 它简化你上面的例子中,通过参数设置为 NULL 来代替 NA 。它不包括所有选项,但会删除作为参数设置为 NULL 如果语句需要不像扔 NA 错误。同时,还应扩展到具有附加功能参数的多个geoms。

I don't know if this is quite what you need - it simplifies your example above, by setting arguments to NULL instead of NA. It doesn't cover every option but removes the need for the if statements as an argument set to NULL doesn't throw an error like NA. Should also be scalable to multiple geoms with additional function arguments.

gg_custom_point <- function(dat, xvar, yvar, gvar=NULL ,grp=NULL , ... ,
                                                 mytheme=NULL) 
 {
  g <- ggplot() +  
  geom_point(data=dat, aes_string(x=xvar, y=yvar, colour=gvar, group=grp), ...) +
  mytheme

  return(g)
  }


gg_custom_point(mtcars, "wt", "mpg")
gg_custom_point(mtcars, "wt", "mpg", "qsec")
gg_custom_point(mtcars, "wt", "mpg", "qsec" , size=10)
gg_custom_point(mtcars, "wt", "mpg", "qsec" , size=10 , mytheme=theme_bw())

这篇关于从自定义函数实现GEOM选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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