r中n个参数的绘图例程 [英] plotting routine for n number of arguments in r
问题描述
我希望绘制以下参数:
weight <- c(102,20,30,04,022,01,220,10)
height <- c(102,20,30,04,022,01,220,10)
catg <- c(102,20,30,04,022,01,220,10)
catg <- matrix(height,nrow = 2)
,这些应绘制在保存在路径中的pdf文件中:
and these should be plotted in a pdf file saved in the path:
FigureFolder <- "C:\\..."
我正在尝试编写一个函数,该函数需要n个输入参数并将其保存到文件中(在本例中,此文件是由FigureFolder定义的).我具有以下功能:
I am attempting to write a function that takes n number of input parameters and saves them into a file (in this case this file if defined by FigureFolder). I have the following function:
Plotting_Function <- function( ...,FigureFolder){
# find the number of input arguments
nargin <- length(as.list(match.call())) -1
nargin <- nargin - 2
variable_list <- list(...)
variable_list <- variable_list[1:nargin]
for (i in 1:length(variable_list)){
if (variable_list[i] == "catg")
routine 1 will go here
if(variable_list[i] != "catg")
routine 2 will go here
}
}
因此,从此处开始,我试图使该函数正常工作,以便如果将变量"catg"插入到函数中,则绘图例程将遵循例程1(在我的情况下为轮廓),否则它将遵循例程2(折线图)如果是y).
So, from here I am trying to make the function work so thatif the variable 'catg' is inserted into the function the plotting routine will follow routine 1 (contour in my case), otherwise it will follow routine 2 (line plot in y case).
但是,我遇到的问题是variable_list返回一个数字,而不是插入的变量的名称,例如
However, the problem I'm having is that variable_list returns a number and not the name of the variable inserted e.g.
Plotting_Function <- function(weight,height,catg,FigureFolder)
variable_list将是1,2和3,而不是体重,身高和体重,因此我不能使用显示的if语句.有人可以提出一种使这项工作可行的方法吗?
variable_list will be 1,2, and 3 not weight, height, and catg, therefore I cannot use the if statement I have shown. Could anyone suggest a method to make this work?
推荐答案
如果我了解您要执行的操作,则可以这样做:
If I understand what you're trying to do, this would work:
Plotting_Function <- function( ... , FigureFolder) {
v_names <- as.list(match.call())
#variable_list <- list(...)
variable_list <- v_names[2:(length(v_names)-1)]
for (i in 1:length(variable_list)) {
if (variable_list[i] == "catg") print("category") # example
#routine 1 will go here
if (variable_list[i] != "catg") print(as.character(variable_list[i][[1]])) # example
#routine 2 will go here
}
}
这篇关于r中n个参数的绘图例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!