循环绘制R中的图形并将其保存为jpeg [英] Plot graphs in R by loop and save it like jpeg

查看:100
本文介绍了循环绘制R中的图形并将其保存为jpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按循环绘制图形.

I am trying to plot graphs by loop.

输入数据:具有相同结尾* depth.txt的表,表中有2个制表符分隔的列:

Input data: Tables, which have the same ending *depth.txt, there are 2 tab delimited columns in the table:

Baba"\t"58.38

Tata"\t"68.38

Mama"\t"30.80

jaja"\t"88.65

输出:对于所有文件(每个轴的x将是表的第一列),我想为每个* depth.txt(每个文件的名称与表的名称相同)获取一个带plot()的jpeg文件.并且y轴为第二列)

OUTPUT: I would like to get a jpeg file with plot() for each *depth.txt (their names will be the same as the tables' names) for all files (axis x will be the first column from the table and axis y will be second column)

我创建了脚本的一部分,但是没有用:

I created a part of the script, but it doesn't work:

files <- list.files(path="/home/fil/Desktop/", pattern="*depth.txt", full.names=T,recursive=FALSE)

for (i in 1:length(files))
plot(read.table(files[i],header=F,sep="\t")$V1,read.table(files[i],header=F,sep="\t")$V2)
dev.copy(jpeg,filename=files[i])
dev.off

它不起作用,您能帮我吗?我是R的初学者.

It doesn't work, could you help me please? I am a beginner with R.

推荐答案

以下内容将满足您的要求吗?

Will the following do what you want?

for (i in 1:length(files)) {
  dat <- read.table(files[i], header = FALSE, sep = '\t')
  jpeg(file = paste(files[i], '.jpeg', sep = ''))
  plot(dat$V1, dat$V2)
  dev.off()
}

这篇关于循环绘制R中的图形并将其保存为jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆