r语言绘制plot图 text函数
文初友情宣传
强烈建议你推荐给身边的科研汪。关注 生信技能树,多一点数据认知,让他们的科研上一个台阶:
,你的生物信息学入门课
,你的生物信息学成长宝藏
// 2020-07-26 //
导读
本文是转载帖,因为今天画图时发现x轴标注文字在坐标轴上,就查了一下,发现网上又已经有人系统地整理好了!人人为我,我为人人,因此共享出来。
正文
Knowledge Points
R语言低级绘图函数 text
text函数用来在一张图表上添加文字,只需要指定对应的x和y坐标,以及需要添加的文字内容就可以了
基本用法:
1
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n")
2
text(x = 3, y = 3, labels = "text")
效果图如下
支持同时创建多个text标签
代码示例:
1
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n")
2
text(x = c(3, 3), y = c(3, 5), labels = c("text", "text"))
效果图如下:
参数调整:
col : 设置文字的颜色
代码示例:
1
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n")
2
text(x = c(3, 3), y = c(3, 5), labels = c("text", "text"), col = c("red", "blue"))
效果图如下:
cex : 设置文字的大小
代码示例:
1
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n")
2
text(x = c(3, 3), y = c(3, 5), labels = c("text", "text"), cex = c( 0.5 , 2 ))
效果图如下:
sort : 对文件进行旋转
代码示例:
1
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n")
2
text(x = 3, y = 3, labels = "text", srt = 45)
效果图如下:
adj : 调整文字的位置,一个值时调整的是x轴的位置,如果为两个值时,第一个调整的是x轴的位置,第二个调整的是y轴的位置,可选范围为[0, 1]
对x轴进行调整,代码示例:
1
par(mfrow = c(1,3))
2
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "adj = 0")
3
abline(h = 3, v = 3 , col = "gray", lty = 3)
4
text(x = 3, y = 3, labels = "text", adj = 0)
5
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "adj = 0.5")
6
abline(h = 3, v = 3 , col = "gray", lty = 3)
7
text(x = 3, y = 3, labels = "text", adj = 0.5)
8
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "adj = 1")
9
abline(h = 3, v = 3 , col = "gray", lty = 3)
10
text(x = 3, y = 3, labels = "text", adj = 1)
x轴调整的效果图如下:
对y轴进行调整,代码示例:
1
par(mfrow = c(1,3))
2
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "adj = 0")
3
abline(h = 3, v = 3 , col = "gray", lty = 3)
4
text(x = 3, y = 3, labels = "text", adj = c(0.5, 0))
5
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "adj = 0.5")
6
abline(h = 3, v = 3 , col = "gray", lty = 3)
7
text(x = 3, y = 3, labels = "text", adj = c(0.5, 0.5))
8
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "adj = 1")
9
abline(h = 3, v = 3 , col = "gray", lty = 3)
10
text(x = 3, y = 3, labels = "text", adj = c(0.5, 1))
y轴调整的效果图如下:
pos: 也是对文字的位置进行调整,不能和adj参数同时使用, 可选值为1, 2, 3, 4, 分别对应下, 上, 左, 右4个方向
代码示例:
1
par(mfrow = c(1, 4))
2
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "pos = 1")
3
abline(h = 3, v = 3 , col = "gray", lty = 3)
4
text(x = 3, y = 3, labels = "text", pos = 1)
5
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "pos = 2")
6
abline(h = 3, v = 3 , col = "gray", lty = 3)
7
text(x = 3, y = 3, labels = "text", pos = 2)
8
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "pos = 3")
9
abline(h = 3, v = 3 , col = "gray", lty = 3)
10
text(x = 3, y = 3, labels = "text", pos = 3)
11
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "adj = 4")
12
abline(h = 3, v = 3 , col = "gray", lty = 3)
13
text(x = 3, y = 3, labels = "text", pos = 4)
效果图如下:
offset : 只有和和pos 函数搭配起来才会产生作用,对文字的文字进行调整
代码示例:
1
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "pos = 1")
2
abline(h = 3, v = 3 , col = "gray", lty = 3)
3
text(x = 3, y = 3, labels = "text", pos = 1, offset = 1, col = "red")
4
text(x = 3, y = 3, labels = "text", pos = 1, offset = -1, col = "blue")
效果图如下:
font: 设置文字的格式,1是默认值,就是普通的文字,2代表加粗,3代表斜体, 4代表加粗+斜体, 5只有用来ADOBE的设备上时,才有用
代码示例:
1
par(mfrow = c(1, 5))
2
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "font = 1")
3
abline(h = 3, v = 3 , col = "gray", lty = 3)
4
text(x = 3, y = 3, labels = "text", font = 1)
5
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "font = 2")
6
abline(h = 3, v = 3 , col = "gray", lty = 3)
7
text(x = 3, y = 3, labels = "text", font = 2)
8
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "font = 3")
9
abline(h = 3, v = 3 , col = "gray", lty = 3)
10
text(x = 3, y = 3, labels = "text", font = 3)
11
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "font = 4")
12
abline(h = 3, v = 3 , col = "gray", lty = 3)
13
text(x = 3, y = 3, labels = "text", font = 4)
14
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "font = 5")
15
abline(h = 3, v = 3 , col = "gray", lty = 3)
16
text(x = 3, y = 3, labels = "text", font = 5)
效果图如下:
text函数的常见用法和参数就参考上面的例子就够了,当我们想打印数学表达式或者一些特殊符号在图片上是,就需要对lables 参数进行特别设置对于数据表达式,使用expression 函数将其转化成表达式后,在显示出来会更好
打印一个开平方的表达式,代码示例:
1
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n")
2
text(x = 3, y = 3, labels = expression(sqrt(x)))
效果图如下:
每一个不曾起舞的日子
都是对生命的辜负
在这美好的人间四月天里,感谢您关注铁柱推文。
这里都是铁柱自己的所感,铁柱怀着感恩的心把这些电子笔记分享给大家。也希望大家喜欢。
课程信息
更新时间:每天不定时
QQ:1432618546
Wechat: tiezhuxiaoketang
Blog:www.tiezhu.club
文字|铁柱
排版|铁柱
图片|还是铁柱
(铁柱不容易呀,在此欢迎有志之士加入)
- E N D -
无论是风里,还是在雨里,我都在这里守候着你~
铁柱永远在路上
我知道你在看哟