R语言自学笔记二:图形初阶

3,866次阅读
22 条评论

1 使用图形

通过逐条输入语句构建图形,逐渐完善图形特征,直至得到想要的效果。
如下

attach(mtcars)
plot(wt, mpg)
abline(lm(mpg-wt))
title("Regression of MPG on weight")
detach(mtcars)

以上代码首句绑定了数据框mtcars。第二条语句打开了一个图形窗口并生成了一幅散点图,横轴表示车
身重量,纵轴为每加仑汽油行驶的英里数。第三句向图形添加了一条最优拟合曲线。第四句添加了标题。最后一句为数据框解除了绑定。
R语言自学笔记二:图形初阶
要通过代码保存图形,将绘图语句夹在开启目标图形设备的语句和关闭目标图形设备的语句之间即可。例如,以下代码会将图形保存到当前工作目录中名为mygraph.pdf的PDF文件中:

pdf("mygraph.paf")
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg-wt))
title("Regression of MPG on weight")
detach(mtcars)
dev.off()

除了pdf(),还可以使用函数win.metafile()、png()、jpeg()、bmp()、tiff()、xfig()和postscript()将图形保存为其他格式。

2 一个简单的例子

dose <- c(20,30,40,45,60)
drugA <- c(16,20,27,40,60)
drugB <- e(15,18,25,31,40)
plot(dose,drugA, type="b")

plot()是R中为对象作图的一个泛型函数(它的输出将根据所绘制对象类型的不同而变化)。本例中,plot(x, y, type=”b”)将x置于横轴,将y置于纵轴,绘制点集(x, y),然后使用线段将其连接。选项type=”b”表示同时绘制点和线。

R语言自学笔记二:图形初阶

3 图形参数

我们可以通过修改称为图形参数的选项来自定义一幅图形的多个特征(字体、颜色、坐标轴、标题)。
一种方法是通过函数par()来指定这些选项。以这种方式设定的参数值除非被再次修改,否则将在会话结束前一直有效。其调用格式为par(optionname=value,optionname=name,…)。不加参数地执行par()将生成一个含有当前图形参数设置的列表。添加参数no.readonly=TRUE可以生成一个可以修改的当前图形参数列表。

opar<- par(no.readonly=TRUE)
par(lty=2,pch=17)
plot(dose, drugA,type="b")
par(opar)

R语言自学笔记二:图形初阶
首个语句复制了一份当前的图形参数设置。第二句将默认的线条类型修改为虚线(lty=2)并将默认的点符号改为了实心三角(pch=17)。然后我们绘制了图形并还原了原始设置。
指定图形参数的第二种方法是为高级绘图函数直接提供optionname=value的键值对。
以上图形可以用以下代码表示

plot(dose,drugA,type="b",1ty=2,pch=17)

3.1 符号和线条

可以使用图形参数来指定绘图时使用的符号和线条类型,表3-2 用于指定符号和线条类型的参数

参 数 描 述
pch 指定绘制点时使用的符号(见图3-4)
pch 指定符号的大小。cex是一个数值,表示绘图符号相对于默认大小的缩放倍数。默认大小为1,1.5表示放大为默认值的1.5倍,0.5表示缩小为默认值的50
lty 指定线条类型(参见图3-5)
lwd 指定线条宽度。lwd是以默认值的相对大小来表示的(默认值为1)。例如,lwd=2将生成一条两倍于默认宽度的线条

选项pch=用于指定绘制点时使用的符号。可能的值如图3-4所示
R语言自学笔记二:图形初阶
你还可以指定边界颜色(col=)和填充色(bg=)。选项lty=用于指定想要的线条类型。可用的值如图3-5所示。综合以上选项,以下代码:

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

将绘制一幅图形,其线条类型为点线,宽度为默认宽度的3倍,点的符号为实心正方形,大小为默认符号大小的2倍。结果如图3-6所示
R语言自学笔记二:图形初阶

R语言自学笔记二:图形初阶

3.2 颜色

R中有若干和颜色相关的参数。表3-3列出了一些常用参数。

参 数 描 述
col 默认的绘图颜色。某些函数(如lines和pie)可以接受一个含有颜色值的向量并自动循环使用。例如,如果设定col=c(“red”, “blue”)并需要绘制三条线,则第一条线将为红色,第二条线为蓝色,第三条线又将为红色
col.axis 坐标轴刻度文字的颜色
col.lab 坐标轴标签(名称)的颜色
col.main 标题颜色
col.sub 副标题颜色
fg 图形的前景色
bg 图形的背景色

在R中,可以通过颜色下标、颜色名称、十六进制的颜色值、RGB值或HSV值来指定颜色。
举例来说,col=1、col=”white”、col=”#FFFFFF”、col=rgb(1,1,1)和col=hsv(0,0,1)
都是表示白色的等价方式。

3.3 文本属性

图形参数同样可以用来指定字号、字体和字样。表3-4阐释了用于控制文本大小的参数。字体族和字样可以通过字体选项进行控制(见表3-5)。

表3-4用于指定文本大小的参数

参 数 描 述
cex 表示相对于默认大小缩放倍数的数值。默认大小为1,1.5表示放大为默认值的1.5倍,0.5表示缩小为默认值的50
cex.axis 坐标轴刻度文字的缩放倍数。类似于cex
cex.lab 坐标轴标签(名称)的缩放倍数
cex.main 标题的缩放倍数
cex.sub 副标题的缩放倍数

表3-5 用于指定字体族、字号和字样的参数

参 数 描 述
font 整数。用于指定绘图使用的字体样式。1=常规,2=粗体,3=斜体,4=粗斜体,5=符号字体(以Adobe符号编码表示)
font.axis 坐标轴刻度文字的字体样式
font.lab 坐标轴标签(名称)的字体样式
font.main 标题的字体样式
font.sub 副标题的字体样式
ps 字体磅值(1磅约为1/72英寸)
family 绘制文本时使用的字体族。标准的取值为serif(衬线)、sans(无衬线)和mono(等宽)

举例:

par(font.1ab=3,cex.lab=1.5, font.main=4,cex.main=2)

创建的所有图形都将拥有斜体、1.5倍于默认文本大小的坐标轴标签(名称),以及粗斜体、2倍于默认文本大小的标题。

3.4 图形尺寸与边界尺寸

可以使用表3-6列出的参数来控制图形尺寸和边界大小。
表3-6 用于控制图形尺寸和边界大小的参数

参 数 描 述
pin 以英寸表示的图形尺寸(宽和高)
mai 以数值向量表示的边界大小,顺序为“下、左、上、右”,单位为英寸
mar 以数值向量表示的边界大小,顺序为“下、左、上、右”,单位为英分*。默认值为c(5, 4, 4, 2) + 0.1
par(pin=c(4,3), mai=c(1,.5, 1,.2))

可生成一幅4英寸宽、3英寸高、上下边界为1英寸、左边界为0.5英寸、右边界为0.2英寸的图形。

4 添加文本、自定义坐标轴和图例

除了图形参数,许多高级绘图函数(例如plot、hist、boxplot)也允许自行设定坐标轴和文本标注选项。举例来说,以下代码在图形上添加了标题(main)、副标题(sub)、坐标轴标签(xlab、ylab)并指定了坐标轴范围(xlim、ylim)。结果如图3-8所示。

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

R语言自学笔记二:图形初阶

4.1 标题

可以使用title()函数为图形添加标题和坐标轴标签。函数title()中亦可指定其他图形参数(如文本大小、字体、旋转角度和颜色)。举例来说,以下代码将生成红色的标题和蓝色的副标题,以及较默认大小小25

title(main= "my Title", col.main="red",
sub="wy Sub-title", col.sub="blue"
xlab="My X labe1",ylab="My Y labe1"
col.lab="green", cex.lab=0.75)

4.2 坐标轴

使用函数axis()来创建自定义的坐标轴,而非使用R中的默认坐标轴
axis(side, at=, labels=, pos=, lty=, col=, 1as=, tek=,…)
R语言自学笔记二:图形初阶

4.3 参考线

函数abline()可以用来为图形添加参考线。

abline(h=e(1,5,7))
abline(v=seq(1, 10, 2),lty=2,col="blue")

在y为1、5、7的位置添加了水平实线,则在x为1、3、5、7、9的位置添加了垂直的蓝色虚线。代码清单3-3为我们的药物效果图在y = 30的位置创建了一条参考线。

4.4 图例

可以使用函数legend()来添加图例
legend(Iocation, title, legend, …)
常用选项详述于表3-8中
R语言自学笔记二:图形初阶

4.5 文本标注

可以通过函数text()和mtext()将文本添加到图形上。text()可向绘图区域内部添加文本,而mtext()则向图形的四个边界之一添加文本。使用格式分别为:
text(1ocation, text to place”, pos, …)
mtext(“text to place”, side, line=n, …)
R语言自学笔记二:图形初阶

attach(mtcars)
plot(wt,mpg,
main="Mileage vs. Car Meignt",
xlab="Weight",ylab="Mileage"
pch=18,col="blue")
text (wt,mmpq,
row.names (mtcare),
cex=0.6, pos=4, co1="red")
detach(mtcars)

以上代码:针对数据框mtcars提供的32种车型的车重和每加仑汽油行驶英里数绘制了散点图。函数text()被用来在各个数据点右侧添加车辆型号。各点的标签大小被缩小了40 R语言自学笔记二:图形初阶

5 图形的组合

可以在par()函数中使用图形参数mfrow=c(nrows, ncols)来创建按行填充的、行数为nrows、列数为ncols的图形矩阵。另外,可以使用nfcol=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)

R语言自学笔记二:图形初阶

正文完
 
评论(22 条评论)
2026-01-05 20:46:17 回复

Thanks for sharing. I read many of your blog posts, cool, your blog is very good.

 Windows  Edge  中国广西玉林市电信
2026-01-16 06:19:18 回复

Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

 Windows  Edge  中国广西玉林市电信
2026-01-19 04:14:57 回复

衷心感谢 你们的付出。确确实实 吸引人。 現代建築 您 真的 分享经验。增加文章!

 Windows  Chrome  乌克兰
2026-01-20 12:27:43 回复

I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

 Windows  Edge  中国广西玉林市电信
2026-01-20 23:14:45 回复

Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

 Windows  Edge  中国广西玉林市电信
2026-01-28 02:59:36 回复

Thanks for sharing. I read many of your blog posts, cool, your blog is very good.

 Windows  Edge  中国广西玉林市电信

Thanks for sharing. I read many of your blog posts, cool, your blog is very good. https://accounts.binance.com/cs/register-person?ref=OMM3XK51

 Windows  Edge  中国广西玉林市电信
2026-02-17 09:26:50 回复

Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

 Windows  Edge  中国广西玉林市电信
2026-02-20 07:53:09 回复

Your article helped me a lot, is there any more related content? Thanks!

 Windows  Edge  中国广西玉林市电信
2026-03-21 04:01:47 回复

Thanks for sharing. I read many of your blog posts, cool, your blog is very good.

 Windows  Edge  中国广西钦州市电信
2026-04-09 08:37:54 回复

Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

 Windows  Edge  中国广西玉林市电信
2026-04-09 22:03:00 回复

Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://www.binance.info/register?ref=QCGZMHR6

 Windows  Edge  中国广西钦州市电信
2026-04-23 01:11:00 回复

Your article helped me a lot, is there any more related content? Thanks!

 Windows  Edge  中国广西玉林市电信
2026-04-29 00:57:29 回复

Your article helped me a lot, is there any more related content? Thanks!

 Windows  Edge  中国广西玉林市电信

Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

 Windows  Edge  中国广西百色市电信
2026-05-14 01:24:17 回复

Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

 Windows  Edge  中国广西玉林市电信
2026-05-17 06:14:00 回复

Thanks for sharing. I read many of your blog posts, cool, your blog is very good.

 Windows  Edge  中国广西玉林市电信
2026-05-18 09:26:11 回复

Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

 Windows  Edge  中国广西钦州市电信
2026-05-20 17:31:20 回复

Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://www.binance.com/futures/ref?code=L4EUT9FG

 Windows  Edge  中国广西玉林市电信
2026-05-24 05:13:13 回复

I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://www.binance.bh/register?ref=IHJUI7TF

 Windows  Edge  中国广西玉林市电信
2026-05-25 09:36:36 回复

I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://www.binance.com/sk/register?ref=WKAGBF7Y

 Windows  Edge  中国广西钦州市电信
2026-06-13 01:32:47 回复

Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://www.binance.bh/register?ref=L4EUT9FG

 Windows  Edge  中国广西玉林市电信