2011-11-07 35 views
7

我有一系列ggplot圖表,我將重複一些小的變化。我想將這些帶有選項的qplots包裝到一個函數中以避免代碼中的重複。在R中使用可選參數書寫ggplot函數

我的問題是,我使用+ facet_wrap()選項的一些圖,但對於其他人我不是。即我需要facet wrap作爲可選參數。當它包含時,代碼需要使用facets參數中提供的變量調用+ facet_wrap()。

所以最好我的功能是這樣的,有刻面作爲一個可選的參數:

$ qhist(variable, df, heading, facets) 

我曾試着用搜索引擎如何添加可選參數,他們建議要麼傳遞缺省值,或者如果循環使用的missing()函數。我一直無法上班。

這是我寫的函數,也包含了可選的facets參數的所需功能。

$ qhist <- function(variable, df, heading, facets) { 
     qplot(variable, data = df, geom = "histogram", binwidth = 2000, 
      xlab = "Salary", ylab = "Noms") + 
     theme_bw() + 
     scale_x_continuous(limits=c(40000,250000), 
       breaks=c(50000,100000,150000,200000,250000), 
       labels=c("50k","100k","150k","200k","250k")) + 
     opts(title = heading, plot.title = theme_text(face = "bold", 
      size = 14), strip.text.x = theme_text(size = 10, face = 'bold')) 
     # If facets argument supplied add the following, else do not add this code 
     + facet_wrap(~ facets) 
+0

「方面」是否合乎邏輯?或者是其他東西? (要面向的變量的字符向量?) –

+0

不,這將是一個變量來面向。舉例來說,我有薪水數據直方圖,有時候我想按行業來分析。 –

回答

11

建立一個默認的方式是這樣的:

testFunction <- function(requiredParam, optionalParam=TRUE, alsoOptional=123) { 
    print(requiredParam) 
    if (optionalParam==TRUE) print("you kept the default for optionalParam") 
    paste("for alsoOptional you entered", alsoOptional) 
} 

* 編輯 *

哦,好......所以我覺得我有更好的想法你在問什麼。它看起來像你不知道如何將可選的方面帶入ggplot對象。這個怎麼樣:

qhist <- function(variable, df, heading, facets=NULL) { 
    d <- qplot(variable, data = df, geom = "histogram", binwidth = 2000, 
     xlab = "Salary", ylab = "Noms") + 
    theme_bw() + 
    scale_x_continuous(limits=c(40000,250000), 
      breaks=c(50000,100000,150000,200000,250000), 
      labels=c("50k","100k","150k","200k","250k")) + 
    opts(title = heading, plot.title = theme_text(face = "bold", 
     size = 14), strip.text.x = theme_text(size = 10, face = 'bold')) 
    # If facets argument supplied add the following, else do not add this code 
    if (is.null(facets)==FALSE) d <- d + facet_wrap(as.formula(paste("~", facets))) 
    d 
    return(d) 
    } 

我還沒有測試過這個代碼。但總體思路是facet_wrap需要一個公式,所以如果facet作爲字符串傳遞,您可以使用as.formula()構建一個公式,然後將其添加到plot對象。

如果我這樣做,我會讓該函數接受一個可選的facet公式,然後將該facet公式直接傳遞到facet_wrap。這將抵消對as.formula()調用將文本轉換爲公式的需要。

+0

歡呼聲,我想我明白了,我想我仍然不明白如何使用該結構,然後在該函數調用的代碼中包含該選項。我會更新我的問題以反映這一點。 –

+0

在你的函數體中,嘗試類似'res < - qplot(...)+ ...;如果(facets)res < - res + facet_wrap(〜facets);返回(res)' – Tyler

+0

鑑於'qplot'中'variable'的非標準評估,你最好使用'ggplot'語法並使用'aes_string':'d < - ggplot(data = df,aes_string(x =變量))+ geom_histogram(binwidth = 2000)+ ...' –

2

請注意,您也可以使用missing(facets)檢查facets參數是否已指定。如果您使用@JD長期的解決方案,這將是這個樣子:

qhist <- function(variable, df, heading, facets) { 
    ... insert @JD Longs' solution ... 

    if (!missing(facets)) d <- d + facet_wrap(as.formula(paste("~", facets))) 
    return(d) 
} 

...請注意,我也從facets=NULL只是facets改變默認參數。

許多R函數使用像這樣的缺失參數,但通常情況下,我傾向於傾向於使用默認參數值(如NULLNA)的@JD Long變體。但有時候沒有很好的默認值...

+0

感謝您提供另一種選擇。我曾經看到過這個,但是我不知道如何將它包含在我的特定功能中。 –

3

也許最好的辦法是停止使用這些不尋常的變量名,包括逗號或空格。

作爲解決方法,這是@ JDLong的答案的擴展。訣竅是重新命名facet變量。

f <- function(dat, facet = NULL) { 
    if(!missing(facet)) { 
     names(dat)[which(names(dat) == facet)] <- ".facet." 
     ff <- facet_wrap(~.facet.) 
    } else { 
     ff <- list() 
    } 
    qplot(x, y, data = dat) + ff 
} 

d <- data.frame(x = 1:10, y = 1:10, "o,o" = gl(2,5), check.names=F) 

f(d, "o,o") 
f(d) 
+0

對不起,'零售貿易'實際上是變量中的一個字符串。該變量本身被命名爲「行業」。 –

+0

然後,它不會導致任何錯誤。也許在你的使用方式上有一些錯誤。你能否附上一個可重現的例子? – kohske

+0

你是對的。我的問題是我沒有把參數作爲一個字符串。 –