## Default S3 method:
plot(x, y = NULL, # x和y坐标
type = "p", # 图的类型
xlim = NULL, ylim = NULL, # x、y的坐标轴范围
log = "", # 对"x"、"y"或"xy" 取对数
main = NULL, # 图的主标题
sub = NULL, # 副标题
xlab = NULL, ylab = NULL, # x、y轴标注
ann = par("ann"), # 逻辑值,是否使用默认的x、y轴标注注释
axes = TRUE, # 逻辑值,是否显示坐标轴, "xaxt" 或 "yaxt" 选择不显示对应坐标轴
frame.plot = axes, # 是否显示图边框
panel.first = NULL, # 表达式,在坐标轴设定后,在绘图之前添加图形,对于添加网格比较有用
panel.last = NULL, # 同上,相反
asp = NA, # y/x 的比例
xgap.axis = NA, # x轴标签显示的距离
ygap.axis = NA,# y轴标签显示的距离
bty = 'o' # 图边框类型
... # 其它参数,见 par函数
)
经常用的参数还有:
col # 颜色
bg # 图形背景色
fg # 图形前景色
pch # 点的形状
cex # 字符或者形状大小
lty # 线形
lwd # 线粗细
...
# 绘制1x3的图
par(mfrow = c(1, 3))
# 参数类型
adj = c(0,0.5,1)
# 绘图
lapply(adj, function(x){
plot(x = mtcars$mpg,y = mtcars$disp,
main = paste('adj is the ',x,' !',sep = ''),
adj = x)
})
# 加载数据
data("mtcars")
# 绘制一行两列
par(mfrow = c(1,1))
# 绘图
plot(x = mtcars$mpg,y = mtcars$disp,
# 绘制点图
type = 'p',
# 主标题
main = 'I am The main Title',
# x坐标轴标签
xlab = 'I am Xlab',
# y坐标轴标签
ylab = 'I am Ylab',
# 点大小
cex = 2,
# 点形状
pch = 19,
# 点颜色
col = 'red',
# 绘制网格线
panel.first = grid(8, 8,col = 'grey',lty = 1),
# 绘制边框
frame.plot = T,
)
# 添加网格
grid(col = 'orange',lty = 1)
看到我们第二次添加的网格线是按照刻度位置添加的,plot 函数里面的不是。
par 函数的参数很多,可以在 plot 之前进行参数设定,还可以使用par(no.readonly = TRUE) 来获取当前图的所有信息。
par 函数主要包括 3 类参数:
1.不可修改参数:
“cin”, “cra”, “csi”, “cxy”, “din” 和 “page”
2.只能通过 par 函数修改的参数:
“ask”,“fig”, “fin”,“lheight”,“mai”, “mar”, “mex”, “mfcol”, “mfrow”, “mfg”,“new”,“oma”, “omd”, “omi”,“pin”, “plt”, “ps”, “pty”,“usr”,“xlog”, “ylog”,“ylbias”
mfcol/mfrow 参数可以设置画板按几行几列分割,用来绘制多个图形。
3.可被其它函数设置的参数:
plot.default, plot.window, points, lines, abline, axis, title, text, mtext, segments, symbols, arrows, polygon, rect, box, contour, filled.contour 和 image
4. 实现在同一个图层上作图
如果想将两个plot的图像叠加在一起,可以在第二个plot前加上
par(new=TRUE)
par(mfrow = c(1,2))
# 坐标轴延长
plot(0:3,0:3,xaxs = 'r',yaxs = 'r')
plot(0:3,0:3,xaxs = 'i',yaxs = 'i')
par(mfrow = c(1,2))
# 坐标轴是否显示
plot(0:3,0:3,xaxt = 'l',yaxt = 'n')
plot(0:3,0:3,xaxt = 'n',yaxt = 's')
par(mfrow = c(1,1))
# 建空画板
plot(x = mtcars$mpg,y = mtcars$disp,type = 'n')
# 添加点
points(x = mtcars$mpg,y = mtcars$disp,
# 形状
pch = 22,
# 边框颜色
col = '#BF61F2',
# 填充颜色
bg = 'orange',
# 大小
cex = 3,
# 边框线粗
lwd = 2)
par(mfrow = c(1,1))
# 建空画板
plot(x = mtcars$mpg,y = mtcars$disp,type = 'n')
# 添加线
lines(x = mtcars$mpg,y = mtcars$disp,
# 线形
lty = 2,
# 边框线粗
lwd = 2,
# 线颜色
col = 'blue'
)
par(mfrow = c(1,1))
# 建空画板
plot(x = mtcars$mpg,y = mtcars$disp,type = 'n')
# 添加横线
abline(h = 250, # Y轴位置
lwd = 5,col = '#9FF048')
# 添加竖线
abline(v = 22.5, # Y轴位置
lwd = 5,col = 'red')
# 添加斜线
abline(a = 100, # y轴截距
b = 7, # 斜率
lwd = 5,col = '#317CDE')
axis(side, # 绘制坐标轴的位置:1 底部,2 左边,3 上边,4 右边
at = NULL, # 刻度线的位置
labels = TRUE, # 是否显示刻度的文字标签,可以为和刻度数量一样额字符向量
tick = TRUE, # 是否显示刻度
line = NA, # 坐标轴距离边界绘制的行数
pos = NA, # 绘制坐标轴的坐标位置
outer = FALSE, # 是否绘制在绘图边距
font = NA, # 文字的字体
lty = "solid", # 线形
lwd = 1, # 线粗
lwd.ticks = lwd, # 刻度线粗
col = NULL, # 线的颜色
col.ticks = NULL, # 刻度线颜色
hadj = NA, padj = NA, # 坐标轴文字(平行和垂直)标签的对齐方式
gap.axis = NA, # 坐标轴标签显示间距
...)
par(mfrow = c(2,2))
# 建空画板
plot(1:3,1:3,type = 'n')
# 上方添加坐标轴
axis(side = 3)
# 建空画板
plot(1:3,1:3,type = 'n')
# 右方添加坐标轴
axis(side = 4)
# 建空画板
plot(1:3,1:3,type = 'n')
# 改变刻度
axis(side = 3,at = seq(1,3,0.4))
# 建空画板
plot(1:3,1:3,type = 'n')
# 添加刻度标签
axis(side = 3,at = seq(1,3,0.5),labels = LETTERS[1:5])
par(mfrow = c(2,2))
# 建空画板
plot(1:3,1:3,type = 'n')
# 添加坐标轴远离图
axis(side = 3,line = 2,lty = 6)
# 建空画板
plot(1:3,1:3,type = 'n')
# 不显示刻度
axis(side = 3,tick = F)
# 建空画板
plot(1:3,1:3,type = 'n',axes = F,frame.plot = T)
# 改变轴线粗细,刻度粗细
axis(side = 1,lwd = 3,lwd.ticks = 6)
# 改变轴线颜色,刻度颜色
axis(side = 2,col = 'red',col.ticks = 'blue')
# 建空画板
plot(1:3,1:3,type = 'n',axes = F,frame.plot = T)
# 轴标签左对齐
axis(side = 1,hadj = 0)
# 轴标签居中对齐
axis(side = 2,hadj = 0.5)
# 轴标签右对齐
axis(side = 3,hadj = 1)
title(main = NULL, # 主标题
sub = NULL, # 副标题
xlab = NULL, # x轴标提
ylab = NULL, # y轴标题
line = NA, # 文字标签远离图的行数
outer = FALSE, # 是否将图放在图边界外
...)
par(mfrow = c(2,2))
# 建空画板
plot(1:3,1:3,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
title(main = 'I am main title',
sub = 'I am sub title',
xlab = 'I am xlab',
ylab = 't am ylab')
# 建空画板
plot(1:3,1:3,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
title(main = 'I am main title',
sub = 'I am sub title',
xlab = 'I am xlab',
ylab = 't am ylab',
# 增加文字距离
line = 3)
# 建空画板
plot(1:3,1:3,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
title(main = 'I am main title',
sub = 'I am sub title',
xlab = 'I am xlab',
ylab = 't am ylab',
# 添加到图外
outer = T)
text(x, ...)
## Default S3 method:
text(x, y = NULL, # 文字的坐标位置
labels = seq_along(x$x), # 字符型量或者表达式
adj = NULL, # 文字对齐方式
pos = NULL, # 文字位置,会覆盖adj参数,1,2,3,4,分别对于:下左上右
offset = 0.5, # 当指定pos后,控制文本标签的相对距离
vfont = NULL,
cex = 1, # 文字大小
col = NULL, # 文字颜色
font = NULL, # 字体
srt = 0, # 文字旋转角度
...)
par(mfrow = c(1,2))
# 建空画板
plot(1:3,1:3,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
text(x = 2,y = 2,
labels = 'I am text !',
cex = 2,
col = 'purple',
srt = 45)
# 建空画板
plot(1:3,1:3,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
# 对齐
text(x = 2,y = 2,
labels = 'I am text !',
cex = 2,
col = 'purple',
adj = 0)
mtext 函数也是添加文本注释的,不同的是在绘图区域外添加文本。参数和 text 函数基本一致。
mtext(text, # 需要添加的文本
side = 3, # 添加的位置,1234分别对应下左上右
line = 0, outer = FALSE,
at = NA, # 指定文字显示的坐标轴位置
adj = NA,
padj = NA, # 和 adj 差不多感觉
cex = NA, col = NA, font = NA, ...)
par(mfrow = c(1,2))
# 建空画板
plot(1:3,1:3,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
mtext(text = 'I am The bottom TEXT!',
line = 2.5,col = 'red',
side = 1)
mtext(text = 'I am The top TEXT!',
line = 2,col = 'blue',
side = 3)
# 建空画板
plot(1:3,1:3,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
mtext(text = 'I am The bottom TEXT!',
line = -10,col = 'red',
# 文字中心从1开始
at = 1,
side = 1)
segments 函数可以添加线段。
segments(x0, y0, # 起始坐标
x1 = x0, y1 = y0, # 终止坐标
col = par("fg"), # 颜色
lty = par("lty"), # 线形
lwd = par("lwd"), # 线粗
...)
par(mfrow = c(1,1))
# 建空画板
plot(1:3,1:3,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
segments(x0 = 1.5,y0 = 1.5,
x1 = 2.8,y1 = 3.0,
col = '#1AC5F0',lty = 1,lwd = 3)
symbols 函数可以添加符号。
symbols(x, y = NULL, # 添加符号的位置
circles, # 绘制圆形的半径
squares, # 绘制方形的边长
rectangles, # 绘制矩形,需要一个两列的matrix,第一列为宽,第二列为高
stars, # 绘制星形,至少需要3列及以上
thermometers, # 有点像温度计的形状,3或4列的matrix
boxplots, # 绘制箱线图,5列的matrix
inches = TRUE,
add = FALSE, # 是否添加到已经存在的图形上
fg = par("col"), # 图形颜色
bg = NA, # 图形填充色
xlab = NULL, ylab = NULL, main = NULL,
xlim = NULL, ylim = NULL, ...)
par(mfrow = c(1,2))
# 建空画板
plot(1:3,1:3,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
# 绘制圆形
symbols(x = 2,y = 2,
add = T, # 添加到已有图形上
circles = 0.5,
fg = 'red',bg = '#FF7ABF')
# 绘制方形
symbols(x = 2,y = 2,
add = F, # 新绘制图形
squares = 0.5,
lwd = 5,
fg = 'green',bg = 'black')
arrows 函数可以在图上添加箭头。
arrows(x0, y0, # 箭头起始位置
x1 = x0, y1 = y0, # 箭头终止位置
length = 0.25, # 箭头头部边长度
angle = 30, # 箭头张开角度
code = 2, # 箭头类型
col = par("fg"), # 箭头颜色
lty = par("lty"), # 线形
lwd = par("lwd"), # 线粗
...)
par(mfrow = c(1,1))
# 建空画板
plot(1:3,1:3,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
# 默认
arrows(x0 = 2,y0 = 1,
x1 = 1,y1 = 2.5,lwd = 3)
# 箭头角度
arrows(x0 = 2,y0 = 1,
x1 = 2,y1 = 2.5,
angle = 60,col = 'red',lwd = 3)
# 箭头角度
arrows(x0 = 2,y0 = 1,
x1 = 3,y1 = 2.5,
angle = 60,col = 'green',length = 0.3,code = 3,lwd = 3)
rect 函数可以绘制矩形。
rect(xleft, ybottom, xright, ytop, # 矩形的左下右上的坐标位置
density = NULL, # 用线条填充矩形
angle = 45, # 线条角度
col = NA, # 矩形填充色
border = NULL, # 矩形边框颜色
lty = par("lty"), # 线形
lwd = par("lwd"), # 线粗
...)
par(mfrow = c(1,2))
# 建空画板
plot(1:10,1:10,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
# 颜色,线条
rect(xleft = 2, ybottom = 2, xright = 8, ytop = 8,
lwd = 3,lty = 5,col = 'orange',border = 'blue')
# 建空画板
plot(1:10,1:10,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
# 斜线填充
rect(xleft = 2, ybottom = 2, xright = 8, ytop = 8,
density = 4,angle = 60)
polygon 函数绘制多边形。
polygon(x, y = NULL, # 多边形的xy坐标
density = NULL, angle = 45,
border = NULL, col = NA, lty = par("lty"),
..., fillOddEven = FALSE)
par(mfrow = c(1,2))
# 建空画板
plot(1:10,1:10,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
polygon(x = 1:9,y = c(1,8,4,6,9,3,5,8,1),
col = '#A2FA73',border = 'black',lwd = 3)
# 建空画板
plot(1:10,1:10,type = 'n',axes = T,frame.plot = T,xlab = '',ylab = '')
# 斜线填充
polygon(x = 1:9,y = c(1,8,4,6,9,3,5,8,1),
density = 3,angle = 60,
col = '#A2FA73',border = 'black',lwd = 3)
box 函数添加边框。
box(which = "plot", # "plot", "figure", "inner" 和 "outer" 字符向量
lty = "solid", # 线形
...)
par(mfrow = c(1,1))
# 建画板
plot(1:7, abs(stats::rnorm(7)), type = "h", axes = FALSE)
axis(1, at = 1:7, labels = letters[1:7])
box(lty = 5, col = 'red')