2016-09-19 68 views
1

我需要帮助来在一个循环中对多个文件进行barplot。我创建了一个功能barplot只有两列,这些不同的文件从5列输入文件中包含的,然后调用函数为条形图创建一个函数并在循环中调用它并保存所有图形

bar.plot <- function(col_name1, col_name2,input_file, lable1, lable2) 

{ 

    barplot(col_name1,names.arg = col_name2, xlab = "label1", ylab = "lable2", 
col= "blue",main = "bar plot of average",border = "red") 

    box() 

    } 

#call the function for 10 files 
    i <- 1 

    for (i in 1:10) 
    { 

    filename <_paste("C:/Users/admin/GoogleDrive/Vidya/R/document/Group_",i,".csv",sep = "") 
    group <- read.csv(filename) 


    lablex <- "average" 

    labley <- "master id" 

    bar.plot(group$total_pause_time,group$employee_id, group, lablex, labley) 

    } 

输出曲线显示xlable作为label1和ylable为lable2,即使我已经进入了“ average“in lablex and”master id“in labley。 也告诉我如何用10个不同的名字保存这些不同的地块,例如plot1.jpg到plot10.jpg

回答

1

这将创建十个单独地块

bar.plot <- function(col_name1, col_name2,input_file, lable1, lable2) 
{ 
    barplot(col_name1,names.arg = col_name2,xlab=label1,ylab=label2, 
col= "blue",main = "bar plot of average",border = "red") 
    box() 
    } 

#call the function for 10 files 
label1 <- "average" 
label2 <- "master id" 

setwd("C:/Users/admin/GoogleDrive/Vidya/R/document/Group_/") 
filename <- list.files(pattern = ".csv") 
myfiles <- lapply(filename, read.csv) 

    for (i in myfiles) 
    { 
    group <- data.frame(myfiles[i]) 
    jpeg(paste(i,".jpg")) 
    bar.plot(group$total_pause_time,group$employee_id,label1,label2) 
    dev.off() 
    } 

Sample plot

+0

嗨,这段代码保存所有绘图在一个pdf中,但仍然标签不来作为“平均”和“主ID” –

+0

@vidyanair希望这可能对你有帮助 –

+0

kumar ok ..我会尝试 –

0

试试这个:

bar.plot <- function(col_name1, col_name2,input_file, lable1, lable2) { 
barplot(col_name1,names.arg = col_name2, xlab = lable1, ylab = lable2, 
col= "blue",main = "bar plot of average",border = "red") 
box() 
} 

#call the function for 10 files 
for (i in 1:10){ 
filename <-paste("C:/Users/admin/GoogleDrive/Vidya/R/document/Group_",i,".csv",sep = "") 
    group <- read.csv(filename) 
lable1 <- "average" 
lable2 <- "master id" 
bar.plot(group$total_pause_time,group$employee_id, group, lable1, lable2) 
dev.copy(jpg,paste("C:/Users/admin/GoogleDrive/Vidya/R/document/plot",i,".jpg) 
dev.off() 
} 
+0

没有working..still显示lable1的PDF文件和lable2,而不是“平均”和“主ID”..和JPG不保存.. –

+0

如果这些文件,组1到组10已经显示在全球环境,那么就没有必要读取.csv ..在这种情况下如何做到这一点..意味着我直接想要从全球环境中导入这些文件同时增加他们的名字lfom group1到group10 –