【问题标题】:Pass variable name to plotting function title将变量名称传递给绘图函数标题
【发布时间】:2012-03-12 11:24:07
【问题描述】:

我想知道是否有人可以帮助我在函数中使用变量名。 我整理了一个点图,对变量进行排序,然后生成位图,但我无法让R 将变量名称传递给plot 标题。

示例数据

id<-c(1,2,3)
blood<-c(1,2,10)
weight<-c(1,2,13)


mydata<-as.data.frame(cbind(id,blood,weight))
mydata$blood

#######SORTED DOT PLOT####


Dplotter<-function (id,x,Title=""){
if (is.null(Title)) {Title=""} else {Title=Title} 

DIR<-paste("C:/temp/WholePlots/New/",Title,".bmp",sep="")

D<-as.data.frame(cbind(id,x))
x1<-as.data.frame(D[order(x),])

bmp(DIR)
dotchart(x1$x,labels=id,main=Title,pch=16)
dev.off()
}


###############
Dplotter(mydata$id,mydata$blood,"Blood")

Dplotter(mydata$id,mydata$weight,"Weight")
  1. 在函数的第二行,我想传递变量 名称,类似

    `if (is.null(Title)) {Title=varname(x)} else {Title=Title}`
    

    这样我就不必在函数标题字段中输入“Blood” (例如 Dplotter(mydata$id,mydata$blood)

    基本上,如何将变量名粘贴到函数中?它 如果能从 标题(没有附加数据集,我被告知这是不好的 练习),这样你就不会得到mydata$blood,而是得到 标题中的“血”。

    我找不到一个简单的解决方案来粘贴变量名 一个函数。你可以猜到,把变量名放在一个 paste() 函数返回变量的值(这样 情节标题用值而不是变量名填充)。

  2. 我还想进一步自动化该功能,以便我可以 只需输入数据集和ID,然后重复该功能 对于数据集中的每个变量。显然这需要解决 先问第1题,否则标题和文件名都会遇到 问题。

【问题讨论】:

    标签: r function variable-names


    【解决方案1】:

    一般答案是deparse(substitute(x))。例如

    fooPlot <- function(x, main, ...) {
        if(missing(main))
            main <- deparse(substitute(x))
        plot(x, main = main, ...)
    }
    

    这里正在使用:

    set.seed(42)
    dat <- data.frame(x = rnorm(1:10), y = rnorm(1:10))
    fooPlot(dat, col = "red")
    

    产生:

    但在您的特定示例中,这不起作用,因为您不希望 dat$x 作为标题,您只需要 x。然而,我们可以做更多的操作:

    fooPlot <- function(x, main, ...) {
        if(missing(main)) {
            main <- deparse(substitute(x))
            if(grepl("\\$", main)) {
                main <- strsplit(main, "\\$")[[1]][2]
            }
        }
        plot(x, main = main, ...)
    }
    

    fooPlot(dat$x, col = "red") 给出:

    请注意,这段代码做了一些假设,main 不是向量,传递给 plot 的对象中只会有一个 $(即,您不能使用嵌套列表,例如上面的代码)。

    【讨论】:

    • 非常感谢。它真的很好用。有没有人对如何做 2 有任何建议(问题 2,见上文)。例如,让 foo plot 在另一个函数“PlotALL”中运行,该函数将数据集的所有变量一一传递给 foo plot?这将允许我编写 ## PlotAll (id,mydata) 并打印所有图表。非常感谢
    【解决方案2】:

    您需要检索一组字符串、变量名称,并将它们用作绘图的标题和文件名。

    我将使用 longley 数据集来说明这个技巧。

    data(longley, package="datasets")
    
    #return a vector with variable names
    colnames(longley)
    names(longley) #equivalent
    
    #get the name of a specific variable (column number):
    names(longley)[1]
    

    要绘制每个变量,获取两组字符串:变量名和文件名:

    var.names=names(longley)
    file.names=paste(var.names, "bmp", sep=".") 
    #with an extra step to prefix a directory to those filenames
    
    for (i in 1:ncol(longley) ) {
    
        bmp(file=file.names[i])
        plot(longley[[i]], main=var.names[i], ylab="")
        dev.off()
    }
    

    ylab="",否则它会给出一个愚蠢的“longley[[i]]”作为 y-label,如果我使用 var.name[i] 作为 ylab,那将是多余的。

    【讨论】:

    • 亲爱的 p_barill,这正是我所需要的。非常感谢您的帮助。
    猜你喜欢
    • 2017-07-21
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2012-04-13
    • 2019-07-30
    • 2017-07-08
    相关资源
    最近更新 更多