本文介绍了用可选参数在R中编写ggplot函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一系列ggplot图表,我正在重复一些小的变化。我想将这些带有选项的qplots包含到一个函数中,以避免代码中的重复。 我的问题是,对于我使用的一些图+ facet_wrap()选项,但对于其他人我不是。即我需要facet wrap作为可选参数。当它包含时,代码需要使用facets参数中提供的变量调用+ facet_wrap()。所以理想情况下,我的函数看起来像这样,facet是一个可选的参数: $ qhist(变量,df,标题,构面) 我试着用Google搜索如何添加可选参数,并且他们建议传递一个默认值或者使用带有missing()函数的if循环。我还没有得到任何工作。 下面是我写的函数,也包含可选的facets参数的所需功能。 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' df,geom =直方图,binwidth = 2000, xlab =薪水,ylab =Noms)+ theme_bw()+ scale_x_continuous(限制= c(40000,250000 ), break = 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') )#如果提供facets参数,则添加以下内容,否则不要添加此代码 + facet_wrap(〜facets) 解决方案设置默认的方式如下所示: testFunction< - function(requiredParam,optionalParam = TRUE,alsoOptional = 123){ print(requiredParam) if(optionalParam == TRUE)print(you保留optionalParam的默认值) paste(也用于您输入的可选项,也可选)} EDIT * 哦,好的...所以我想我有更好地了解你在问什么。它看起来像你不知道如何将可选的方面带入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 (限额= c(40000,250000),中断= c(50000,100000,150000,200000,250000),标签= c(50k,100k,150k, (标题=标题,plot.title = theme_text(face =bold, size = 14),strip.text.x = theme_text(size = 200k,250k))+ opts 10,face ='bold'))#如果提供facets参数,则添加以下内容,否则不要添加此代码 if(is.null(facets)== FALSE)d d return(d)} 我还没有测试过这个代码。但总体思路是facet_wrap需要一个公式,所以如果facet作为字符串传递,你可以使用 as.formula()来构建公式,然后添加它到情节对象。 如果我这样做,我会让该函数接受一个可选的构面公式,然后将该构面公式直接传递到 facet_wrap 。这将消除 as.formula()调用将文本转换为公式的需要。 I have a series of ggplot graphs that I'm repeating with a few small variations. I would like to wrap these qplots with their options into a function to avoid a lot of repetition in the code.My problem is that for some of the graphs I am using the + facet_wrap() option, but for others I am not. I.e. I need the facet wrap to be an optional argument. When it is included the code needs to call the +facet_wrap() with the variable supplied in the facets argument.So ideally my function would look like this, with facets being an optional argument:$ qhist(variable, df, heading, facets)I have tried googling how to add optional arguments and they suggest either passing a default value or using an if loop with the missing() function. I haven't been able to get either to work.Here is the function that I have written, with the desired functionality of the optional facets argument included too. $ 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) 解决方案 the way to set up a default is like this: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)}*EDIT*Oh, ok... so I think I have a better idea of what you are asking. It looks like you're not sure how to bring the optional facet into the ggplot object. How about this: 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) }I have not tested this code at all. But the general idea is that the facet_wrap expects a formula, so if the facets are passed as a character string you can build a formula with as.formula() and then add it to the plot object. If I were doing it, I would have the function accept an optional facet formula and then pass that facet formula directly into the facet_wrap. That would negate the need for the as.formula() call to convert the text into a formula. 这篇关于用可选参数在R中编写ggplot函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-15 22:29