图中的动态变量名,文件和与循环的兼容性

时间:2014-12-15 11:37:03

标签: r dynamic plot filenames

我正在尝试编写一个绘制绘图的函数并自动将其保存到文件中。 我努力做到这两点动作的伎俩[plotname = varname& filename = varname&], 并使其与从循环中调用它兼容。

# Create data
my_df = cbind(uni=runif (100),norm=rnorm (100),bino=rbinom(100,20, 0.5));   head (my_df)
my_vec = my_df[,'uni']; 

# How to make plot and file-name meaningful if you call the variable in a loop?

# if you call by name, the plotname is telling. It is similar what I would like to see.
hist(my_df[,'bino'])


for (plotit in colnames(my_df)) {
    hist(my_df[,plotit])
    print (plotit)
    # this is already not meaningful
}



# step 2 write it into files 
hist_auto <-  function(variable, col ="gold1", ...) {
    if ( length (variable) > 0 ) {
        plotname = paste(substitute(variable), sep="", collapse = "_");     print (plotname); is (plotname)
        # I would like to define plotname, and later tune it according to my needs
        FnP = paste (getwd(),'/',plotname, '.hist.pdf', collapse = "", sep=""); print (FnP)
        hist (variable, main = plotname)
        #this is apparently not working: I do not get my_df[, "bino"] or anything similar
        dev.copy2pdf (file=FnP )
    } else { print ("var empty") }
}


hist_auto (my_vec)
# name works, and is meaningful [as much as the var name ... ]

hist_auto (my_df[,'bino'])
# name sort of works, but falls apart

assign (plotit, my_df[,'bino'])
hist_auto (get(plotit))
# name works, but meaningless


# Now in a loop

for (plotit in colnames(my_df)) {
    my_df[,plotit]
    hist(my_df[,plotit])
    ## name works, but meaningless and NOT UNIQUE > overwritten by next
}


for (plotit in colnames(my_df)) {
    hist_auto(my_df[,plotit])
    ## name works, but meaningless and NOT UNIQUE > overwritten by next
}

for (plotit in colnames(my_df)) {
    assign (plotit, my_df[,plotit])
    hist_auto (get(plotit))
    ## name works, but meaningless and NOT UNIQUE > overwritten by next
}

我的目标是拥有一个迭代的功能,例如。矩阵的列,绘图并保存每个具有唯一且有意义的名称。

该解决方案可能涉及到substitute()parse()eval()和paste()的智能组合,但缺乏可靠的理解我无法弄清楚。

我的实验基础是: how to dynamically call a variable?

2 个答案:

答案 0 :(得分:1)

这样的事情怎么样?您可能需要install.packages("ggplot2")

library(ggplot2)
my_df <- data.frame(uni=runif(100),
                    norm=rnorm(100),
                    bino=rbinom(100, 20, 0.5))
get_histogram <- function(df, varname, binwidth=1, save=T) {
    stopifnot(varname %in% names(df))
    title <- sprintf("Histogram of %s", varname)
    p <- (ggplot(df, aes_string(x=varname)) +
          geom_histogram(binwidth=binwidth) +
          ggtitle(title))
    if(save) {
        filename <- sprintf("histogram_%s.png", gsub(" ", "_", varname))
        ggsave(filename, p, width=10, height=8)
    }
    return(p)
}
for(var in names(my_df))
    get_histogram(my_df, var, binwidth=0.5)  # If you want to save them
get_histogram(my_df, "uni", binwidth=0.1, save=F)  # If you want to look at a specific one

答案 1 :(得分:0)

所以我最终得到了两个函数,一个可以迭代数据帧,另一个函数需要单个向量。使用Adrian的部分[谢谢!]解决方案:

hist_dataframe <- function(variable, col ="gold1", ...) { stopifnot(colName %in% colnames(df)) variable = df[,colName] stopifnot(length (variable) >1 ) plotname = paste(substitute(df),'__', colName, sep="") FnP = paste (getwd(),'/',plotname, '.hist.pdf', collapse = "", sep=""); print (FnP) hist (variable, main = plotname) dev.copy2pdf (file=FnP ) }

简单向量的那个保留在Q中。