x<-c(1:10) y<-x z<-10/x opar<-par(no.readonly = T) par(mar=c(5,4,4,8)+0.1) plot(x,y,type="b",pch=23,col="red",yaxt="n",lty=3,ann=F) lines(x,z,type = "b",pch=22,col="blue",lty=2) axis(2,at=x,labels = x,col.axis="red",las=2) axis(4,at=z,labels = round(z,digits = 2),col.axis="blue",las=2,cex.axis=0.7,tck=-.01) mtext("y=10/x",side = 4,line = 3,cex.lab=1,las=2,col="blue") title("An example of crestive axes",xlab="X value",ylab="Y=X") par(opar)
我们可以通过函数text()和mtext()将文本添加到图形中。text可以向绘图区域内部添加文本,而mtext则向图形的四个边界之一添加文本。使用格式分别如下:
例:
opar<-par(no.readonly = T) #生成一个可以修改的当前图形参数表
par(cex=1)
plot(1:7,1:7,type="n")
text(3,3,"Example of default text")
text(4,4,family="mono","Example of mono-spaced text")
text(5,5,family="serif","Example of serif text")
par(opar)
下面简单介绍图形组合:
attach(mtcars)
opar<-par(no.readonly = T)
par(mfrow=c(2,2))
#2行2列的图形矩阵
#2行2列的图形矩阵
plot(wt,mpg,main = "Scatterplot of vs.mpg")
plot(wt,disp,main = "Scatterplot of vs.disp")
hist(wt,main="Histogram of wt")
boxplot(wt,main="Boxplot of wt")
par(opar)
detach(mtcars)
attach(mtcars)
layout(matrix(c(1,1,2,3),2,2,byrow=T))
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars)
attach(mtcars)
layout(matrix(c(1,1,2,3),2,2,byrow=T),
widths = c(3,1),heights = c(1,2))
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars)
opar <- par(no.readonly=T)
par(fig=c(0,0.8,0,0.8))
plot(mtcars$wt,mtcars$mpg,
xlab="Miles per Gallon",
ylab="Car weight")
par(fig=c(0,0.8,0.55,1),new=T)
boxplot(mtcars$wt,horizontal=T,axes=F)
par(fig=c(0.65,1,0,0.8),new=T)
boxplot(mtcars$mpg,axes=F)
mtext("Enhenced Scatterplot",side=3,outer=T,line=-3)
par(opar)
本篇内容摘自《R语言实战》第三章:图形初阶
2016年8月30日补充:
> hh <- hist(Nile) #得到直方图的同时,hist()函数也有一返回值列表> hh $breaks [1] 400 500 600 700 800 900 1000 1100 1200 1300 1400 $counts [1] 1 0 5 20 25 19 12 11 6 1 $density [1] 0.0001 0.0000 0.0005 0.0020 0.0025 0.0019 0.0012 0.0011 0.0006 0.0001 $mids [1] 450 550 650 750 850 950 1050 1150 1250 1350 $xname [1] "Nile" $equidist [1] TRUE attr(,"class") [1] "histogram"