将变量名称传递给绘图函数标题

时间:2021-12-19 05:56:38

I was wondering if anyone could help me use a variable name within a function. I've put together a dot plot that sorts variables and then produces a bitmap, but I can't get R to pass the variable name to the plot title.

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

Example data

示例数据

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. In the second line of the function, I'd like to pass on the variable name, something like

    在函数的第二行,我想传递变量名,比如

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

    so that I don't have to put "Blood" in the function Title field (e.g. Dplotter(mydata$id,mydata$blood)

    所以我不必在函数Title字段中输入“Blood”(例如Dplotter(mydata $ id,mydata $ blood)

    Basically, how does one paste in the variable name in a function? It would be even better if one could take out the dataset name from the Title (without attaching the dataset, which I've been told is bad practice), so that instead of getting mydata$blood, you just get "blood" in the title.

    基本上,如何在函数中粘贴变量名?如果可以从标题中取出数据集名称(没有附加数据集,我被告知这是不好的做法)会更好,这样你就可以得到“血液”,而不是获得mydata $ blood。标题。

    I've failed to find an easy solution to paste in a variable name in a function. As you can guess, putting the variable name in a paste() function returns the values of the variable (so that the plot title is filled with values rather the variable name).

    我没能找到一个简单的解决方案来粘贴函数中的变量名。您可以猜测,将变量名称放在paste()函数中会返回变量的值(以便使用值而不是变量名称来填充图表标题)。

  2. I'd also like to automate the function even further, so that I can just put the dataset and the ID,and then have the function repeated for each variable in the dataset. Obviously this requires solving question 1 first, otherwise both title and filenames will encounter problems.

    我还希望进一步自动化该功能,这样我就可以放置数据集和ID,然后为数据集中的每个变量重复该功能。显然,这需要首先解决问题1,否则标题和文件名都会遇到问题。

2 个解决方案

#1


13  

The general answer is deparse(substitute(x)). E.g.

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

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

Here it is in use:

它在使用中:

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

Which produces:

哪个产生:

将变量名称传递给绘图函数标题

In your particular example though, this won't work because you don't want dat$x as the title, you want just x. We could do a little more manipulation however:

但是在你的特定例子中,这不起作用,因为你不希望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, ...)
}

Which for fooPlot(dat$x, col = "red") gives:

对于fooPlot(dat $ x,col =“red”)给出了:

将变量名称传递给绘图函数标题

Note this code makes some assumptions, that main is not a vector, that there will only ever be one $ in the object passed to plot (i.e. you couldn't use a nested list for example with the above code).

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

#2


1  

You need to retrieve a set of strings, the variable names, and use them for the title of your plots and filenames as well.

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

I will use the longley dataset to illustrate the trick.

我将使用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]

To plot each variable, get two sets of strings: variable names and filenames:

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

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="", since otherwise it gives a silly "longley[[i]]" as y-label, and if I use var.name[i] as ylab, it would be redundant.

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

#1


13  

The general answer is deparse(substitute(x)). E.g.

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

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

Here it is in use:

它在使用中:

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

Which produces:

哪个产生:

将变量名称传递给绘图函数标题

In your particular example though, this won't work because you don't want dat$x as the title, you want just x. We could do a little more manipulation however:

但是在你的特定例子中,这不起作用,因为你不希望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, ...)
}

Which for fooPlot(dat$x, col = "red") gives:

对于fooPlot(dat $ x,col =“red”)给出了:

将变量名称传递给绘图函数标题

Note this code makes some assumptions, that main is not a vector, that there will only ever be one $ in the object passed to plot (i.e. you couldn't use a nested list for example with the above code).

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

#2


1  

You need to retrieve a set of strings, the variable names, and use them for the title of your plots and filenames as well.

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

I will use the longley dataset to illustrate the trick.

我将使用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]

To plot each variable, get two sets of strings: variable names and filenames:

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

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="", since otherwise it gives a silly "longley[[i]]" as y-label, and if I use var.name[i] as ylab, it would be redundant.

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