106) 凡例の追加


105のコードを改変して、折れ線グラフに凡例を追加します。

#Data
groups<-c("A","B","C")
x<-c(0,1,2,4)
y1<-c(1.1,1,0.9,1.2)
y2<-c(1,2,3,4)
y3<-c(1.2,3,4,5)
z1<-c(0.1,0.1,0.15,0.2)
z2<-c(0.1,0.2,0.3,0.3)
z3<-c(0.1,0.2,0.3,0.5)
#Plot
plot(x,y1,type="l",pch=21,bg="white",
xlim=c(0,4),ylim=c(0,5),axes=FALSE,ann=FALSE)
arrows(x,y1,x,y1+z1,length=0.05,angle=90)
points(x,y1,type="o",pch=21,bg="white")
box()
par(new=T)
plot(x,y2,type="l",pch=21,bg="black",
xlim=c(0,4),ylim=c(0,5),axes=FALSE,ann=FALSE)
arrows(x,y2,x,y2+z2,length=0.05,angle=90)
points(x,y2,type="o",pch=21,bg="black")
par(new=T)
plot(x,y3,type="l",pch=21,bg="red",
xlim=c(0,4),ylim=c(0,5)axes=FALSE,ann=FALSE)
arrows(x,y3,x,y3+z3,length=0.05,angle=90,xpd=NA)
points(x,y3,type="o",pch=21,bg="red")
#Y-axis
axis(side=2,at=seq(0,5,1),las=1
mtext("Plasma Y (ng/ml)",side=2,line=2)
#X-axis
axis(side=1,at=seq(0,4,1))
mtext("Time (h)",side=1,line=2)
#Title
mtext("Effect of A or B on Y secretion",side=3,line=1)
#Legend
legend("topleft",legend= groups,pch=21,pt.bg=c("black","red","white"))


Rの新規文書ファイルにコピーして、すべての行を選択して、編集メニューから実行を選択します。

凡例はコマンド"legend()"で表現します。


次のページ