以plot函数为例,plot函数默认画出散点图,例如以下代码
R
# Input data for drug example
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")
#plot一般常见参数为(x,y,type)
#type中的参数 "p"表示散点图, "l"表示线条, "b"表示同时显示散点和线条, "c"表示通过线条连接的空心点, "o"表示覆盖的散点和线条, "s"和"S"表示阶梯状线条, "h"表示类似直方图的垂直线条。"n"不生成任何点或线条。

额外:par()函数
通过函数par()来指定选项。说人话就是创建一个图形参数的副本,以便能够随时还原该图形样式,以这种方式设定的参数值除非被再次修改,否则将在会话结束前一直有效。其调用格式为:
R
par(optionname=value,optionname=name,...)
不加参数地执行par()将生成一个含有当前图形参数设置的列表。添加参数no.readonly=TRUE可以生成一个可以修改的当前图形参数列表。
例如:
R
opar <- par(no.readonly=TRUE) # 生成一个副本
par(lty=2, pch=17) # 改变图片参数
plot(dose, drugA, type="b") #绘图

当我们执行原本创建的副本参数后,图片会被还原
R
par(opar) #将图形参数还原
plot(dose, drugA, type="b")
常用图形设置
(一)线条和符号

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

(二)颜色
例如:
R
# 创建示例数据
data <- c(10, 20, 15, 25, 30)
# 绘制柱状图并指定填充颜色
barplot(data,col="yellow")
(三)文本属性

例如:
R
par(cex.axis=.75,cex.lab=0.5)
发现相对于原本的图,这里面坐标轴的标签刻度都缩小了
同时,我们也可以用font系列函数指定字体族、字号和字样

例如
R
par(font.axis=4,font.lab=4) #均设置为粗斜体
(四)图形尺寸和边界尺寸

例如
R
par(pin=c(2, 3))

R
# 设置图形边缘尺寸
par(mai = c(1, 1, 1, 1)) # 设置绘图区域的边缘尺寸为1英寸,分别对应上、右、下、左边缘
par(mar = c(3, 3, 2, 2)) # 设置整个图形设备的边缘尺寸为3英寸,分别对应上、右、下、左边缘
# 绘制散点图
plot(x, y, type = "b", pch = 19, col = "blue")

(五)添加文本、自定义坐标轴和图例
1.可使用title()函数为图形添加标题和坐标轴标签,调用格式:
title()
函数:
main
参数用于指定主标题。sub
参数用于指定副标题。xlab
参数用于指定x轴标签。ylab
参数用于指定y轴标签
R
x <- 1:10
y <- x^2
plot(x, y, type = "b", pch = 19, col = "blue",xlab = "", ylab = "")
#定义新坐标轴名称时,原来的坐标轴名称需要变为空,否则会导致的新旧坐标轴重合
title(main = "Scatter plot of x and y",
sub = "Relationship between x and y",
xlab = "X values",
ylab = "Y values")

2.可以使用函数axis()来创建自定义的坐标轴,而非使用R中的默认坐标轴,其格式为:
axis()
函数:side
参数用于指定坐标轴的位置(1:底部,2:左侧,3:顶部,4:右侧)。at
参数用于指定刻度线的位置。labels
参数用于指定刻度线的标签。- 其他参数如
pos
、lty
、col
、las
、tck
等可根据需要进行设
例如:
R
x <- c(1:10)
y <- x
z <- 10/x
opar <- par(no.readonly=TRUE) # make a copy of current settings
par(mar=c(5, 4, 4, 8) + 0.1)
plot(x, y, type="b",
pch=21, col="red",
yaxt="n", lty=3, ann=FALSE)
axis(2, at=x, labels=x, col.axis="red", las=2)
axis(4, at=z,
labels=round(z, digits=2),#将第二个y轴上的标签设置为z向量的四舍五入值,并将小数位数限制为两位
col.axis="blue", las=2,#las代表旋转角度
cex.axis=0.7, tck=-.01)

参数设置

3.函数abline()可以用来为图形添加参考线,其使用格式为:
R
abline(h = yvalues, v = xvalues)
具体而言:
h
参数用于指定水平参考线的位置,可以传入一个包含要绘制水平参考线位置的向量。v
参数用于指定垂直参考线的位置,同样可以传入一个包含要绘制垂直参考线位置的向量
例如
R
x <- 1:10
y <- x^2
plot(x, y, type = "b", pch = 19, col = "blue")
# 添加水平参考线
abline(h = c(20, 40, 60), col = "red", lty = 2)
# 添加垂直参考线
abline(v = c(3, 6, 9), col = "green", lty = 3)

4.我们可以使用函数legend()来添加图例,其使用格式为: legend(location,
title, legend, ...)
例如
R
x <- 1:10
y1 <- x^2
y2 <- 2*x
plot(x, y1, type = "b", pch = 19, col = "blue", ylim = c(0, max(y1, y2)))
lines(x, y2, type = "b", pch = 21, col = "red")
# 添加图例
legend("topleft", title = "Legend", legend = c("y = x^2", "y = 2x"), col = c("blue", "red")

参数设置

5.text()
函数用于向绘图区域内部添加文本。它可以在指定的(x, y)坐标位置添加文本,并可以通过其他参数设置文本的颜色、大小、字体等属性
例如:
R
x <- 1:10
y <- x^2
plot(x, y, type = "b", pch = 19, col = "blue")
# 在指定位置添加文本
text(5, 50, "Some Text", col = "red", cex = 1.2)

6.mtext()
函数用于向图形的边缘添加文本。它可以在指定边缘(上、右、下、左)的指定位置添加文本,并可以通过其他参数设置文本的颜色、大小、字体等属性。
例如
R
x <- 1:10
y <- x^2
plot(x, y, type = "b", pch = 19, col = "blue")
# 在右边界上方添加文本
mtext("Right Text", side = 4, line = 2, col = "red", cex = 1.2)

7.par()函数中使用图形参数 mfrow=c(nrows,ncols) 来创建按行填充的、行数为
nrows、列数为ncols的图形矩阵。另外,可以使用 mfcol=c(nrows,ncols) 按列填充矩阵
例如:
R
x <- 1:10
y <- x^2
# 创建按行填充的图形矩阵
par(mfrow = c(2, 2))
plot(x, y, type = "b", pch = 19, col = "blue", main = "Plot 1")
hist(x, main = "Histogram")
boxplot(x, main = "Boxplot")
barplot(x, main = "Barplot")

R
# 创建按列填充的图形矩阵
par(mfcol = c(2, 2))
plot(x, y, type = "b", pch = 19, col = "blue", main = "Plot 1")
hist(x, main = "Histogram")
boxplot(x, main = "Boxplot")
barplot(x, main = "Barplot")
