R base图形绘制

2023-02-27 00:00:00 文本 绘制 坐标轴 刻度 图形

1绘图函数常见的参数
add 设置是将图添加到设备已有的图上,还是先清除设备上的图
axes 控制是否要画坐标图
log 控制是否要对坐标轴刻度做对数变换
type 控制图的类型
type p 散点,l直线,o带实心点的直线, b带空心点的直线,
c间断的直线 s右上折线 S 左上直线 直立线
xlab,ylab x轴和y轴的标签
main 图的主标题
sub 图的副标题
attach(mtcars)

plot(wt,mpg)

abline(lm(mpg~wt))

title(“Regression of MPG om Weight”)

detach(mtcars)

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”)

opar <- par(no.readonly = TRUE)

符号和线条
lty为线条类型
plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=0,cex=0.5)

plot(dose,drugA,type = “b”,lty=2,lwd=1,pch=0,cex=0.5)

plot(dose,drugA,type = “b”,lty=3,lwd=1,pch=0,cex=0.5)

plot(dose,drugA,type = “b”,lty=4,lwd=1,pch=0,cex=0.5)

plot(dose,drugA,type = “b”,lty=5,lwd=1,pch=0,cex=0.5)

plot(dose,drugA,type = “b”,lty=6,lwd=1,pch=0,cex=0.5)

lwd为线条宽度
plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=0,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=2,pch=0,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=3,pch=0,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=4,pch=0,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=5,pch=0,cex=0.5)

pch为指定绘制点时使用的符号
plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=0,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=1,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=2,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=3,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=4,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=5,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=6,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=7,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=8,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=9,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=10,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=11,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=12,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=13,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=14,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=15,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=16,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=17,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=18,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=19,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=21,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=22,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=23,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=24,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=25,cex=0.5)

cex 为符号的大小
plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=0.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=0.8)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=1)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=1.25)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=1.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=1.75)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=2)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=2.25)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=2.5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=2.75)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=3)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=5)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=10)

plot(dose,drugA,type = “b”,lty=1,lwd=1,pch=20,cex=20)

#颜色
col默认的绘图颜色,col.axis 坐标轴刻度文字的颜色,col.lab坐标轴标签(名称)的颜色,col.main标题颜色,col.sub副标题的颜色,

fg图形的前背景,bg图形的背景色
col=1,col=“white”,col=“#FFFFFF”,col=rgb(1,1,1),col=hsv(0,0,1),rainbow(),heat.colors(),terrain.colors(),topo.colors(),cm.colors()
#文本属性
cex 表示相对于默认大小缩放倍数的数值,cex.axis坐标轴刻度文字的缩放倍数,cex.lab 坐标轴标签(名称)的缩放倍数,cex.main标题的缩放倍数
cex.sub副标题的缩放倍数,font整数,用于指定绘图使用的字体样式,1=常规,3=粗体,3=斜体,4=粗斜体,5=符号字体
font.axis坐标轴刻度文字的字体样式,font.lab坐标轴标签(名称)的字体样式,font.main标题的字体样式,font.sub副标题的字体样式
ps 字体磅值,family绘制文本时使用的字体族。标准取值为serif(衬线)、sans(无衬线)、mono(等宽)

图形尺寸与边界尺寸 pin 以英寸表示的图形尺寸,mai以数值向量表示的边界大小 下左上右 ,mar以数值向量表示的边界大小 下左上右
dose <- c(20,30,40,45,60)

drugA <- c(16,20,27,40,60)

drugB <- c(15,18,25,31,40)

type=“b” 表示同时绘制点和图。
plot(dose,drugA,type = “b”)

生成一个可修改的当前图形参数列表
opar <- par(no.readonly = TRUE)

par(pin=c(2,3),lwd=2,cex=1.5,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”,fg=“yellow”)

添加文本、自定义坐标轴和图例
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))

标题
title() main sub xlab ylab
坐标轴
axis() side,一个整数,表示在图形的哪边绘制绘制坐标轴(1=下,2=左,3=上,4=右)
at 一个数值型向量,表示需要绘制刻度线的位置
labels 一个字符型向量,表示置于刻度线旁边的文字标签(如果为NULL,则将直接使用at中的值)
pos 坐标轴线绘制位置的坐标(即与另一条坐标轴相交位置的值)
lty 线条类型
col 线条和刻度线颜色
las 标签是否平行于(=0)或垂直于(=2)坐标轴

自定义坐标轴的示例
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”,# 绘制x对y的图形

yaxt= “n”,lty = 3, ann = FALSE) # 表示绘图时,纵轴不显示刻度线和刻度标签,只留纵轴的框架线,这样在接下来可以利用axis()自定义纵轴

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)

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”)

参考线
abline(h=yvalues,v=xvalues)
abline(h=c(1,5,7)) # y实水平线

abline(v=seq(1,10,2),lty=2,col=“blue”) # x 蓝色垂直虚线

图例
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) # 增加线条、文本、符号、标签的宽度或大小

plot(dose,drugA,type = “b”,

pch=15,lty=1,col=“red”,ylim = c(0,60),

main = “Drug A vs. 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=.05) # 添加次要刻度线

legend(“topleft”,inset = .05,title = “Drug Type”,c(“A”,“B”),

lty = c(1,2),pch = c(15,17),col = c(“red”,“blue”)) # 添加图例

par(opar)

文本标注 text mtext text()可以向绘图区域内部添加文本,mtext向图形的四个边界之一添加文本
text(location,“text to palce”,pos,…) location 文本的位置参数,可为一对x,yx坐标,也可以指定location为locator(1)使用鼠标交互确认摆放
mtext(“text to place”,side,line=n,…) pos 文本相对于位置参数的方位。1=下,2=左,3=上,4=右,side 指定用来放置文本的边。
attach(mtcars)

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=1.5)

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)

图形的组合 par 中mfrow=c(nrows,ncols)来创建按行填充、行数为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)

函数layout()调用形式为layout(mat),其中的mat是一个矩阵,它指定了所要组合的多个图形的所在位置。在以下代码中,一幅图被置于行,
另外两幅图被置于第二行
attach(mtcars)

layout(matrix(c(1,1,2,3),2,2,byrow = TRUE))

hist(wt)

hist(mpg)

hist(disp)

detach(mtcars)

利用widths和heights何以更加地控制每幅图的大小
attach(mtcars)

layout(matrix(c(1,1,2,3),2,2,byrow = TRUE),

widths = c(3,1),heights = c(1,2))

hist(wt)

hist(mpg)

hist(disp)

detach(mtcars)
————————————————



本文来源:https://blog.csdn.net/weixin_45463545/article/details/126228313

相关文章