ハムスターに飼われる院生のブログ

自分用メモが中心のブログです。

複数のファイルから読み込んだ結果を重ねてプロットするスクリプト

Rで頻繁に作図をするという現状、
都度打ち込むよりは多少一般化したスクリプトを作っておいた方が楽そうだという動機を持ったので
以下のようなRスクリプトを作成した。以下に示す。
ここでは複数のファイル(output1.txt~outputN.txt)のうち、1列目のデータをx、2列目のデータをyとして線グラフを作成している。

graphics.off()
#グラフの目盛りに関する設定
x_bottom<-0
x_top<-100
y_bottom<-0
y_top<-100

#凡例に関する設定
legend_vec<-c("legend1","legend2","legend3")

#plot開始
loop<-1:length(legend_vec)
for(i in loop){
	filename<-paste("output",i,".txt",sep="")
	data<-read.table(filename,sep="\t",skip=1)
	plot(data[,1],data[,2],type="l",col=i,
	xlim=c(x_bottom,x_top),ylim=c(y_bottom,y_top),	
		xlab="xlab[-]",ylab="ylab[-]")
	par(new=T)
}
legend("topright",col=r,legend=legend_vec,pch=numeric(length(loop))+16)

凡例の数にあわせてループをまわしてくれる。