par(……,no.readonly=FALSE)
参数:
..arguments in tag = value form, or a list of tagged values. The tags must come from the names of graphical parameters described in the ‘Graphical Parameters’ section.(图形参数选项,一般用tag=value或向量形式展示,如col="black")
no.readonlylogical; if TRUE and there are no other arguments, only parameters are returned which can be set by a subsequent par() call on the same device.(如取值为TRUE,则可修改当前图形中的参数)
图形参数:
1、关于颜色
bg:背景色(在绘图函数中为图形填充色,在par函数中为整个图形的背景色) fg:前景色 col:图形颜色,一般用法:col=1,或col="red",或col=rgb(1,1,1),或col=hsv(0,0,1)
col.axis:坐标轴刻度文字颜色 col.main:主标题颜色 col.sub:副标题颜色 col.lab:标签颜色
code及示例:
dose<-c(20,30,40,45,60)
drugA<-c(16,20,27,40,60)
opar<-par(bg="gray",no.readonly = TRUE)
layout(matrix(c(1,2,3,4,5,6,7,7,7),3,3,byrow = TRUE))
plot(dose,drugA,type="p",main = "type=p")
plot(dose,drugA,type="l",fg="orange",main = "type=l and fg=orange")
plot(dose,drugA,type="b",col="orangered",main = "type=b and col=orangered")
plot(dose,drugA,type="c",col.axis="yellow",main = "type=c and col.axis=yellow")
plot(dose,drugA,type="o",col.lab="red",main = "type=o and col.lab=red")
plot(dose,drugA,type="h",col.main="red",main = "type=h")
plot(dose,drugA,type="s",col.sub="green",main = "type=s",sub = "sub=green")
2、关于字体
family :全局字体,特指字体的类型,标准取值:serif,sans,mono
font :字体,特指字体的形态,如斜体还是粗体(1:常规,2:粗体,3:斜体,4:粗斜体,5:符号字体)
font.axis:坐标轴刻度字体 font.lab:坐标轴标签字体 font.main:主标题字体 font.sub:副标题字体
code及示例:
opar<-par(bg="gray",no.readonly = TRUE)
par(mfrow=c(2,1))
dose<-c(20,30,40,45,60)
drugA<-c(16,20,27,40,60)
drugB<-c(15,18,25,31,40)
par(family="mono") #mono字体
plot(dose,drugA,type="p")
title(main="family arial style",
sub="clinical trials for drugA ")
par(family="")#默认字体
plot(dose,drugA,type="l",fg="orange")
title(main="type=l and fg=orange",sub = "clinical trials for drugA ")
par(opar)
3、关于大小
cex:相对于图形默认大小缩放倍数的数值。默认值为1,1.5为放大默认值的1.5倍,0.5为缩小默认值的50%;
cex.axis:坐标轴字体缩放倍数; cex.lab:坐标轴标签缩放倍数 cex.main:主标题缩放倍数
cex.sub:副标题缩放倍数
ps:字体磅值(文本最终磅值为ps*cex)
code及示例:
opar<-par(no.readonly = TRUE)
par(mfrow=c(2,1))
dose<-c(20,30,40,45,60)
drugA<-c(16,20,27,40,60)
#默认状态
par(family="mono")
plot(dose,drugA,type="p")
title(main="family arial style",
sub="clinical trials for drugA ")
#字体缩放
par(family="mono")
par(cex.axis=1.5,cex.lab=2,cex.sub=1.5,cex.sub=0.8,ps=10)
plot(dose,drugA,type="p")
title(main="family arial style",
sub="clinical trials for drugA ")
par(opar)
4、关于线条及符号
pch: 绘制点时使用的符号 lty:指定线条类型 lwd:线条宽度(默认值1,lwd=2表示为默认值的2倍)
附:
code及示例
dose<-c(20,30,40,45,60)
drugA<-c(16,20,27,40,60)
par(family="mono")
plot(dose,drugA,type="b",pch=21,col="red",bg="orange",lty=3,lwd=2,main="family arial style",
sub="clinical trials for drugA ",cex.axis=1.5,
cex.lab=2,cex.sub=1.5,cex.sub=0.8,ps=10)
5、关于坐标轴,及其标签、范围(1)xlab及ylab: x、y轴的标签,可改变默认值 xlim:x轴数据范围如xlim=c(0,60) ylim:y轴数据范围如ylim=c(0,70)(2)axis(side,at=,labels=,pos=,lty=,col=,las=,tck=,outer=,font=,lty=,lwd=,lwd.tick=,col=,col.tick=,……)
side:整数,表示在图形的哪边绘制坐标轴(1=下,2=左,3=上,4=右)
at:数值型向量,表示需要绘制刻度线的位置
labels:坐标轴刻度线旁的文字标签,如为NULL,则将使用at中的值;
pos:坐标轴线绘制位置的坐标(即与另一条坐标轴线相交位置的值);
las:标签是否平行于(=0)或垂直于(=2)坐标轴;
tck:刻度线长度(负值表示在图形外侧,正值表示在图形内侧,0表示禁用刻度,1表示绘制网格线,默认值为-0.01)(3)次要刻度线(Hmisc包对于最新的r版本已不适用)
code及示例(1)
dose<-c(20,30,40,45,60)
drugA<-c(16,20,27,40,60)
par(family="mono")
plot(dose,drugA,type="b",pch=21,col="red",bg="orange",lty=3,lwd=2,main="family mono style",
sub="clinical trials for drugA ",xlab="dosage",ylab="drug reponse",xlim=c(0,60),ylim=c(0,70)
,cex.axis=1.5,cex.lab=2,cex.sub=1.5,cex.sub=0.8,ps=10)
(2)
opar<-par(no.readonly = TRUE)
par(mar=c(5,4,4,8)+0.1)
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",pch=21,cex=0.5,col="red",xaxt="n",bg="red",lty=3,lwd=2,ann = FALSE)
lines(dose,drugB,type = "b",pch=22,cex=0.6,col="orangered",bg="orangered",lty=3)
axis(1,at=dose,label=dose,col.axis="red",las=0,tck=-0.01)
axis(4,at=drugB,labels = drugB,col.axis="blue",las=2,tck=-0.01)
mtext("drugB",side=4,line=3,las=2,col="blue")
title("An example of drug respose", xlab=" dose value",ylab="drugA")
par(opar)
6、关于图形尺寸及边界mai:设定图形四周的空白尺寸,单位是英寸,以mai=c(bottom,left,top,right)形式使用;
mar:同mai,设定图形四周的空白尺寸,以mar=c(bottom,left,top,right)形式使用,默认为mar=c(5,4,4,2)+0.1;
oma:设定文本四周的空白区域,以oma=c(bottom,left,top,right)形式使用,如下图:
omi:同oma,但单位是英寸;
omd:设定坐标轴内图形距离坐标轴的空白区域参数,以omd=c(x1,x2,y1,y2)形式使用;
code及示图:
opar<-par(no.readonly = TRUE)
par(mai=c(1,1,1,2))
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",pch=21,cex=0.5,col="red",xaxt="n",bg="red",lty=3,lwd=2,ann = FALSE)
lines(dose,drugB,type = "b",pch=22,cex=0.6,col="orangered",bg="orangered",lty=3)
axis(1,at=dose,label=dose,col.axis="red",las=0,tck=-0.01)
axis(4,at=drugB,labels = drugB,col.axis="blue",las=2,tck=-0.01)
mtext("drugB",side=4,line=3,las=2,col="blue")
title("An example of drug respose", xlab=" dose value",ylab="drugA")
par(opar)
7、关于多图形布局
(1)mfrow=c(nrows,ncols)按行填充的、行数为nrows、列数为ncols的图形矩阵;
(2)nfcol=c(nrows,ncols)按列填充矩阵;
(3)layout(mat, widths = rep.int(1, ncol(mat)),heights = rep.int(1, nrow(mat)), respect = FALSE)
mat:矩阵,用以指定图形绘制的位置;
widths:各列宽度值组成的一个向量;
heights:各行高度值组成的一个向量;(相对宽度可以通过数值直接指定,绝对宽度通过lcm()来指定)布局解析请参见:http://blog.sina.com.cn/s/blog_5de124240101pxvm.html
(4)fig=c(x1,x2,y1,y2)具体意义如下:
code及图形示例dose<-c(20,30,40,45,60)
drugA<-c(16,20,27,40,60)
drugB<-c(15,18,25,31,40)
par(fig=c(0,0.8,0,0.8),new=FALSE)
plot(dose,drugA,type = "b",pch=21,cex=0.5,col="red",xaxt="n",bg="red",lty=3,lwd=2,ann = FALSE)
lines(dose,drugB,type = "b",pch=22,cex=0.6,col="orangered",bg="orangered",lty=3)
par(fig=c(0,0.8,0.69,0.95),new=TRUE)
boxplot(drugA, horizontal=TRUE, axes=FALSE)
par(fig=c(0.7,0.95,0,0.8),new=TRUE)
boxplot(drugB, axes=FALSE)
mtext("An example of drug respose", side=3, outer=TRUE, line=-3)
8、关于图例、文本及其他
(1)text(location,"text to place",pos,……)(2)mtext("text to place",side,line=n,……)
另:其他常用选项还有cex、col、font
(3)legend(location,title,legend,.....)
drugA<-c(16,20,27,40,60)
drugB<-c(15,18,25,31,40)
par(fig=c(0,0.8,0,0.8),new=FALSE)
plot(dose,drugA,type = "b",pch=21,cex=0.5,col="red",xaxt="n",bg="red",lty=3,lwd=2,ann = FALSE)
lines(dose,drugB,type = "b",pch=22,cex=0.6,col="orangered",bg="orangered",lty=3)
legend("topleft",inset = .05,title = "drug type",c("A","B"),lty = c(3,3),pch = c(21,22),col=c("red","red"))
par(fig=c(0,0.8,0.69,0.95),new=TRUE)
boxplot(drugA, horizontal=TRUE, axes=FALSE)
par(fig=c(0.7,0.95,0,0.8),new=TRUE)
boxplot(drugB, axes=FALSE)
mtext("An example of drug respose", side=3, outer=TRUE, line=-3)
9、关于par()的其他说明函数par()中的参数可以分为三大类:
(1)只能读取,不能进行设置。包括参数cin,cra,csi,cxy,din。
(2)只能通过函数par()进行设置。包括参数:"ask","fig","fin","lheight","mai","mar","mex","mfcol","mfrow","mfg","new","oma","omd","omi","pin","plt","ps","pty","usr","xlog","ylog"
(3)剩下的参数除了函数par()外,还可以通过各种高级绘图函数进行设置,如函数plot,points,lines,abline,title,text,axis,image,box,contour,rect,arrows等。
点赞 00
分享 收藏1 回贴
相关帖子
运营方向就业班,成为互联网大厂需要的人宝洁图形推理题
调节作用图形解读因子分析中的biplot图形
eviews图形分析SAS 制作图形以及各种高分辨率图形
plant simulation 图形资料库关于基金回撤图形的问题
倾向匹配得分的图形图形编辑软件labview应用
9.png (34.63 KB)