#R语言图形
 #图形初阶
 #使用图形
 attach(mtcars)
 plot(wt,mpg)
 abline=(lm(mpg~wt))
 title("Regression of MPG on weight")
 detach(mtcars)
 #保存
 #这样会将图形保存到当前工作目录中名为mygraph.pdf的pdf文件中
 pdf("mygraph.pdf")
 attach(mtcars)
 plot(wt,mpg)
 abline=(lm(mpg~wt))
 title("Regression of MPG on weight")
 detach(mtcars)
 dev.off()
 #还可以用win.metafile()/png()/bmp()/保存到其他格式
 #执行plot()、hist()或boxplot()这样的高级绘图命令创造新图形时,通常会覆盖先前图
 #第一种,创建一个新图形之前打开一个新的图形窗口:
 dev.new()
 statements to create graph 1
 dev.new()
 statements to create graph 2
 etc.
 #对于windows,history中的recording点上一个和下一个查看已绘制的图形
 #示例如下
 dose=c(20,30,40,45,60)
 drugA=c(16,20,27,40,60)
 drugB=c(15,18,25,31,40)
 plot(dose,drugA,type="b")
 #图形参数
 ##我们可以通过修改称为图形的参数的选项来自定义一幅图形的多个特征(字体、颜色、坐标轴、标题)
 ##一种方法是通过函数par()来指定这些选项,以这种方式设定的参数值除非被再次修改,否则将在会话结束前一直有效
 opar=par(no.readonly = TRUE)#no.readonly=TRUE意思是生成可以修改的当前图形参数列表
 par(lty=2,pch=17)
 plot(dose,drugA,type="b")
 par(opar)
 #pch指定绘制点时使用的符号
 #cex指定符号的大小
 #lty指定线条类型
 #lwd指定线条宽度
 ##颜色
 #col默认的绘图颜色
 #col.axis坐标轴刻度文字的颜色
 #col.lab坐标轴标签的颜色
 #col.main标题颜色
 #col.sub副标题颜色
 #fg图形的前景色,bg图形的背景色
 #在R中可以通过颜色下标、颜色名称、十六进制、rgb或hsv值来指定颜色
 ##创造连续性颜色向量的函数rainbow()/heat.colors()/terrain.colors()等
 #多阶灰度色可使用gary()
 n=10
 mycolors=rainbow(n)
 pie(rep(1,n),labels=mycolors,col=mycolors)
 mygrays=gray(0:n/n)
 pie(rep(1,n),labels=mygrays,col=mygrays)
 ##文本属性
 #指定文本大小的参数cex,坐标轴刻度文字的缩放倍数cex.axis
 #cex.axis坐标轴标签,cex.main主标题的缩放倍数,cex.sub副标题的缩放倍数
 #用于指定字体族字号字样是font##图形尺寸与边界尺寸
 #pin以英寸表示的图形尺寸
 #mai mar 都是以数值向量表示的边界大小
 dose=c(20,30,40,45,60)
 drugA=c(16,20,27,40,60)
 drugB=c(15,18,25,31,40)
 par(pin=c(2,3))
 par(lwd=2,cex=1.5)
 par(cex.axis=.75,font.axis=3)
 plot(dose,drugA,type="b",pch=19,lty=2,col="red")
 plot(dose,drugB,type="b",pch=23,lty=6,col="blue",bg="green")
 par(opar)##添加文本、自定义坐标和图列
 plot(dose,drugA,type="b",col="red",lty=2,pch=2,lwd=2,main="Clinical trials for Drug A ",sub="This is hypothetical data",xlab="Dosage",ylab="Drug Response",xlim=c(0,60),ylim=c(0,70))
 ##标题title()
 ##坐标轴axis()
 ##例子
 x=c(1:10)
 y=x
 z=10/x
 opar=par(no.readonly = TRUE)
 par(mar=c(5,4,4,8)+0.1)
 plot(x,y,type="b",pch=21,col="red",yaxt="n",
      lty=3,ann=FALSE)
 lines(x,z,type="b",pch=22,col="blue",lty=2)
 axis(2,at=x,labels=x,col.axis="blue",lty=2)
 axis(4,at=z,labels=round(z,digits=2),col.axis="blue",las=2,cex.axis=0.7,tck=-.01)
 mtext("y=1/x",side=4,line=3,cex.lab=1,las=2,col="blue")#边界添加文本
 title("An example of creative axes",
       xlab="x values",
       ylab="y=x")
 par(opar)
 ##次要刻度线minor.tick()
 ##参考线abline
 ##图例legend
 #示例
 dose=c(20,30,40,45,60)
 drugA=c(16,20,27,40,60)
 drugB=c(15,18,25,31,40)
 opar=par(no.readonly = TRUE)
 par(lwd=2,cex=1.5,font.lab=2)
 plot(dose,drugA,type="b",
      pch=15,lty=1,col="red",ylim=c(0,60),
      main="Drug A vs. Drug B",
      xlab="Drug Dosage",ylab="Drug Resonse")
 lines(dose,drugB,type="b",pch=17,lty=2,col="blue")
 abline(h=c(30),lwd=1.5,lty=2,col="gray")
 minor.tick(nx=3,ny=3,tick.ratio =0.5)
 legend("topleft",inset=.05,title="Drug Type",c("A","B"),lty=c(1,2),
        pch=c(15,17),col=c("red","blue"))
 par(opar)
 ##文本标注text()是向绘图区域内mtext()是四个边界之一添加文本
 attach(mtcars)
 plot(wt,mpg,main="Mileage vs. Car weight",
      xlab="weight",ylab="Mileage",
      pch=18,col="blue")
 text(wt,mpg,row.names(mtcars),cex=0.6,pos=4,col="red")
 ##数学标注 用plotmatch()
 ##图形的组合
 #你可以在par()函数中使用图形参数mfrow=c(nrows,ncols)来创建按行填充
 #行数为nrow列数为ncols的图形矩阵、nfcol=c(nrows,ncols)是按列来填充
 #例子
 attach(mtcars)
 opar=par(no.readonly = TRUE)
 par(mfrow=c(2,2))
 plot(wt,mpg,main="Scatterplot of wt vs. mpg")
 plot(wt,disp,main = "scatterplot of wt vs disp")
 hist(wt,main="Histogram of wt")
 boxplot(wt,main="Boxplot of wt")
 par(opar)
 detach(mtcars)
 #第二个例子,三行一列
 attach(mtcars)
 opar=par(no.readonly = TRUE)
 par(mfrow=c(3,1))
 hist(wt)
 hist(mpg)
 hist(disp)
 par(opar)
 detach(mtcars)
 #layout()是指定了所要组合的多个图形的所在为止
 attach(mtcars)
 layout(matrix(c(1,1,2,3),2,2,byrow = TRUE))
 hist(wt)
 hist(mpg)
 hist(disp)
 detach(mtcars)
 ##多副图的精细控制fig