首页 > > > R语言实战-第三章实践笔记
1.保存图形
1).通过代码保存图形:
pdf("mygraph.pdf")
attach(mtcars)
plot(wt,mpg)
abline(lm(mpg~wt))
title("Regression of MPG on weight")
detach(mtcars)
dev.off()

用以下代码创建描述药物A的剂量和响应关系的图形:
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()来修改图形参数(字体、颜色、坐标轴、标签),不加参数执行par()将生成一个含有当前图形参数设置的列表,添加参数no.readonly=TRUE可生成一个可以修改的当前图形参数列表。继续刚刚的例子:
opar<-par(no.readonly = TRUE)
par(lty=2,pch=17)
plot(dose,drugA,type = "b")
par(opar) #还原原始设置




plot(dose,drugA,type = "b",lty=3,lwd=3,pch=15,cex=2)


安装RColorBrewer包后,可使用brewer.pal(n,name)来创建颜色值的向量,例:
library(RColorBrewer)
n<-7
mycolors<-brewer.pal(n,"Set1")
barplot(rep(1,n),col=mycolors)

n<-10
mygrays<-gray(0:n/n)
pie(rep(1,n),labels=mygrays,col = mygrays)
3.3.3文本属性

par(font.lab=3,cex.lab=1.5,font.main=4,cex.main=2)
attach(mtcars)
plot(wt,mpg)
abline(lm(mpg~wt))
title("Regression of MPG on weight")
detach(mtcars)

3.3.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(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))

3.4.1标题
title()函数可为图形添加标题和坐标轴标签。
3.4.2坐标轴
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="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=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)

3.4.3参考线
abline()函数可为图形添加参考线。
3.4.4图例
legend()函数可添加图例,使用格式为legend(location,title,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)
par(pin=c(3,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 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(locator(1),title = "Drug Type",cex=.5,
c("A","B"),lty = c(1,2),
pch = c(15,17),col=c("red","blue"))
#通过鼠标单击给出图例位置
par(opar)

text()和mtext()可将文本添加到图形上,text可向绘图区域内部添加文本,而mtext()则向图形的四个边界之一添加文本。
attach(mtcars)
par(pin=c(3,2))
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")
detach(mtcars)

opar<-par(no.readonly = TRUE)
par(cex=.75)
plot(1:7,1:7,type = "n")
text(3,3,"Example of default text")
text(4,4,family="mono","Example of monp-spaced text")
text(5,5,family="serif","Example of serif text")
par(opar)

在par()函数中使用参数mfrow=c(nrows,ncols)或mfcol=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)

attach(mtcars)
layout(matrix(c(1,1,2,3),2,2,byrow = TRUE))
#c(1,1,2,3)以因子形式表示图形
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars)

opar<-par(no.readonly = TRUE)
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.45,1),new=TRUE)
boxplot(mtcars$wt,horizontal = TRUE,axes=FALSE)
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mtcars$mpg,axes=FALSE)
mtext("Enhanced Scatterplot",side=3,outer=TRUE,line = -3)
par(opar)

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3118296/ ,如需转载,请注明出处,否则将追究法律责任。