来自 R语言实战 第三章 图形初阶 示例代码
#####################第三章 图形初阶######################
3.4 添加文本,自定义坐标轴和图例
plot,hist,boxplot可允许自行设置坐标轴和文本标注
plot(dose,drugA,type = "b",
pch=2,lty=2,lwd=2,col="red",
main="Clinical Trials for Durg A",
sub="This is hypothetical data",
xlab="Dosage",ylab="Drug Response",
xlim=c(0,60),ylim=c(0,70))
3.4.1 标题
plot(dose,drugA,type = "b",ann = F) #ann = F取消默认的标题标签
title(main = "main title",col.main="red",
sub="subtile",col.sub="blue",
xlab="xlab",ylab="ylab",
col.lab="green",cex.lab="1.5")
3.4.2 自定义坐标轴
x <- c(1:10)
y <- x
z <- 10/x
opar <- par(no.readonly = T)
par(mar=c(5,6,4,6)+0.1)
plot(x,y,type = "b",
pch=21,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=-0.01)
mtext("y=x",side=2,line = 3,cex.lab=1,las=2,col = "red")
mtext("y=1/x",side=4,line = 3,cex.lab=1,las=2,col = "blue")
title("An example of create Axes",
xlab = "X values")
par(opar)
dev.off()
#添加次要刻度线
#if(F){install.packages("Hmisc")}
#library(Hmisc)
#minor.tick(nx=2,ny=2,tick.ratio = 0.5)
3.4.3 参考线
abine(h=yvalue,v=xvalue) #添加参考线
3.4.4 图例
rm(list = ls())
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 = T)
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 Response")
lines(dose,drugB,type = "b",
pch=17,lty=2,col="blue")
abline(h=c(30),lwd=1.5,lty=2,col="green")
library(Hmisc)
minor.tick(nx=3,ny=2,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"),cex=0.4) ##cex设置图例大小很重要
par(opar)
dev.off()
3.4.5 文本标注
txt()绘图区域内添加文本 mtxt()四个边界之一添加文本
attach(mtcars)
plot(wt,mpg,
main = "Mile vs. Car Weight",
xlab = "Weight",ylab = "Mileage",
pch=18,col="blue")
text(wt,mpg,
rownames(mtcars),
cex=0.6,pos = 4,col="red")
detach()
dev.off()
opar <- par(no.readonly = T)
par(cex=0.8)
par(pin=c(3,4))
par(mar=c(4,4,3,2))
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)
dev.off()
3.4.6 数学标注
demo(plotmath) ##查看数学符号输入表 使用函数plotmath()
3.5 图形组合
- 通过par(mfrow=c(2,2))组合的四幅图
attach(mtcars)
opar <- par(no.readonly = T)
par(mfrow=c(2,2)) #创建按行填充,行列都为2的图形矩阵
plot(wt,mpg,main = "Sccatterplot of wt vs. mpg")
plot(wt,disp,main = "Sccaterplot of wt vs. disp")
hist(wt,main = "Histogram of wt")
boxplot(wt,main="Boxplot of wt")
par(opar)
detach(mtcars)
dev.off()
- par(mfrow=c(3,1)) 使得三行一列排布三幅图形
attach(mtcars)
opar <- par(no.readonly = T)
par(mfrow=c(3,1))
hist(wt)
hist(mpg)
hist(disp)
par(opar)
detach(mtcars)
dev.off()
- layout()函数指定多个图形所在位置
函数layout()的调用形式为layout(mat),其中的mat是一个矩阵,它指定了所要组合的多个图形的所在位置。
一幅图被置于第1行,另两幅图则被置于第2行, 设置第1行中图形的高度是第2行中图形高度的三分之一。右下角图形的宽度是左下角图形宽度的四分之一
attach(mtcars)
layout(matrix(c(1,1,2,3),nrow = 2,ncol=2,byrow = T),
widths = c(3,1),heights=c(1,2))
hist(wt)
hist(mpg)
hist(disp)
detach(mtcars)
dev.off()
- fig()精细控制组合图形
opar <- par(no.readonly = T)
par(fig=c(0,0.8,0,0.8))
attach(mtcars)
plot(wt,mpg,
xlab = "Miles Per Gallon",
ylab = "Car Weight")
par(fig=c(0,0.8,0.55,1),new=T)
boxplot(wt,horizontal = T,axes=F)
par(fig=c(0.65,1,0,0.8),new=T)
boxplot(mpg,axes=F)
mtext("Enhenced Scatterplot",side = 3,outer = T,line = -3)
par(opar)
dev.off()
第一个fig=将散点图设定为占据横向范围00.8,纵向范围00.8
上方的箱线图横向占据00.8,纵向0.551。右侧的箱线图横向占据0.651,纵向00.8。fig=默认会新建一幅图形,所 以在添加一幅图到一幅现有图形上时,请设定参数new=TRUE。
网友评论