3.1使用圖形參數(shù)控制圖形外觀
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(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,drugA,type="b",pch=23,lty=6,col="blue",bg="green")
par(opar)
3.2自定義坐標(biāo)軸
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)#添加x對1|x的線
axis(2,at=x,labels=x,col.axis="red",las=2)#效果:添加了y軸刻度線
axis(4,at=z,labels=round(z,digits=2),col.axis="blue",las=2,cex.axis=0.7,tck=-.1)#添加了z的每個(gè)點(diǎn)的坐標(biāo)
mtext("y=1/x",side=4,line=3,cex.lab=1,las=2,col="blue")#添加了zy=1/x的注釋
title("AN Exampke of C reative Axes",xlab="x value",ylab="Y=X")
par(opar)
3.4.4圖例
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="DrugA vs DrugB",xlab="Drug Dosage",ylab="Drug Response")
lines(dose,drugB,type="b",pch=17,lty=2,col="blue")
abline(h=c(30),lwd=1.5,lty=2,col="gray")
library(Hmisc)
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)
3.4.5文本標(biāo)注
install.packages("rJava")
library("rJava")
library("xlsxjars")
library("xlsx")
setwd("e:/r")
getwd()
attach(mtcars)
plot(wt,mpg,main="Mileage vs. Car Weight",xlab="weight",ylab="mmmm",pch=18,col="blue")
text(wt,mpg,row.names(mtcars),cex=0.6,pos=4,col="red")
opar<-par(no.readonly=TRUE)
par(cex=1.5)
plot(1:7,1:7,type="n")
text(3,3,"EEEEE")
text(4,4,family="mono","EEEE")
text(5,5,family="serif","EEEE")
par(opar)
3.5圖形的組合-多圖形布局的精密控制
opar<- par(no.readonly=TRUE)
#設(shè)置散點(diǎn)圖
par(fig=c(0,0.8,0,0.8))#布局P66
attach(mtcars)
plot(mtcars$wt,mtcars$mpg,xlab="miles per gallon",ylab="car weight")
#在上方添加散點(diǎn)圖
par(fig=c(0,0.8,0.55,1),new=TRUE)
boxplot(wt,horizontal=TRUE,axes=FALSE)
#在右側(cè)添加散點(diǎn)圖
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mpg,axes=FALSE)
mtext("EEEEE",side=3,outer=TRUE,line=-3)
par(opar)