R语言低级绘图函数-text

text函数用来在一张图表上添加文字,只需要指定对应的x和y坐标,以及需要添加的文字内容就可以了

基本用法:

plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n")
text(x = 3, y = 3, labels = "text")

效果图如下:

支持同时创建多个text标签

代码示例:

plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n")
text(x = c(3, 3), y = c(3, 5), labels = c("text", "text"))

效果图如下:

参数调整:

col : 设置文字的颜色

代码示例:

plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n")text(x = c(3, 3), y = c(3, 5), labels = c("text", "text"), col = c("red", "blue"))

效果图如下:

cex : 设置文字的大小

代码示例:

plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n")
text(x = c(3, 3), y = c(3, 5), labels = c("text", "text"), cex = c( 0.5 , 2 ))

效果图如下:

sort : 对文件进行旋转

代码示例:

plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n")
text(x = 3, y = 3, labels = "text", srt = 45)

效果图如下:

adj : 调整文字的位置,一个值时调整的是x轴的位置,如果为两个值时,第一个调整的是x轴的位置,第二个调整的是y轴的位置,可选范围为[0, 1]

对x轴进行调整,代码示例:

par(mfrow = c(1,3))
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "adj = 0")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", adj = 0)
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "adj = 0.5")
abline(h = 3, v = 3 , col = "gray", lty = 3)
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 = 1")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", adj = 1)

x轴调整的效果图如下:

对y轴进行调整,代码示例:

par(mfrow = c(1,3))
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "adj = 0")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", adj = c(0.5, 0))
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "adj = 0.5")
abline(h = 3, v = 3 , col = "gray", lty = 3)
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 = 1")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", adj = c(0.5, 1))

y轴调整的效果图如下:

pos: 也是对文字的位置进行调整,不能和adj参数同时使用, 可选值为1, 2, 3, 4, 分别对应下, 上, 左, 右4个方向

代码示例:

par(mfrow = c(1, 4))
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "pos = 1")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", pos = 1)
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "pos = 2")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", pos = 2)
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "pos = 3")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", pos = 3)
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "adj = 4")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", pos = 4)

效果图如下:

offset : 只有和和pos 函数搭配起来才会产生作用,对文字的文字进行调整

代码示例:

plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "pos = 1")abline(h = 3, v = 3 , col = "gray", lty = 3)text(x = 3, y = 3, labels = "text", pos = 1, offset = 1, col = "red")text(x = 3, y = 3, labels = "text", pos = 1, offset = -1, col = "blue")

效果图如下:

font: 设置文字的格式,1是默认值,就是普通的文字,2代表加粗,3代表斜体, 4代表加粗+斜体, 5只有用来ADOBE的设备上时,才有用

代码示例:

par(mfrow = c(1, 5))
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "font = 1")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", font = 1)
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "font = 2")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", font = 2)
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "font = 3")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", font = 3)
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "font = 4")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", font = 4)
plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", main = "font = 5")
abline(h = 3, v = 3 , col = "gray", lty = 3)
text(x = 3, y = 3, labels = "text", font = 5)

效果图如下:

text函数的常见用法和参数就参考上面的例子就够了,当我们想打印数学表达式或者一些特殊符号在图片上是,就需要对lables 参数进行特别设置

对于数据表达式,使用expression 函数将其转化成表达式后,在显示出来会更好

打印一个开平方的表达式,代码示例:

plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n")
text(x = 3, y = 3, labels = expression(sqrt(x)))

效果图如下:

对于表达式的打印,具体的可以参考 plotmath 函数的帮助文档

时间: 2024-10-08 19:28:10

R语言低级绘图函数-text的相关文章

R语言低级绘图函数-axis

axis函数用来在一张图表上添加轴线,区别于传统的x轴和y轴,axis 允许在上,下,左, 右4个方向添加轴线 以x轴为例,一条轴线包含3个元素,水平的一条横线,叫做axis line , 刻度线, 叫做tick line, 对应的标签 labels 基本用法: 通过side 参数设置需要添加的轴线的方向,从下边开始,沿逆时针方向,数字为1到4 代码示例: par(oma = c(1, 1, 1, 1), mfrow = c(1, 4)) plot(1:5, 1:5, xlim = c(0,6)

R语言低级绘图函数-grid

grid 函数用来在一张图表上添加网格线, 基本用法: plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n") grid() box() 效果图如下:

R语言低级绘图函数-title

title 函数用来在一张图表上添加标题 基本用法: main 表示主标题,通常位于图像的上方, sub 表示副标题,位于图像的下方, xlab 表示x轴的标签,ylab 表示y轴的标签 par(oma = c(1, 1, 1, 1)) plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n", xlab = "", ylab = "") box(which = "figure&q

R语言低级绘图函数-rect

rect 函数用来在一张图上添加矩形,只需要指定左下角和右上角的坐标的位置,就可以画出一个矩形 基本用法: plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), type = "n") rect(xleft = 1, ybottom = 1, xright = 5, ytop = 5) 效果图如下: xleft, ybottom, xright, ytop 支持一次设置多个值,同时创建多个矩形,用法如下: plot(1:5, 1:5, xlim =

R语言hist绘图函数

hist 用于绘制直方图,下面介绍每个参数的作用: 1)x: 用于绘制直方图的数据,该参数的值为一个向量 代码示例: data <- c(rep(1, 10), rep(2, 5), rep(3, 6)) hist(data) 效果图如下: 从图中可以看出,横坐标为不同的区间,纵坐标为落入该区间内的频数: 2) break : 该参数的指定格式有很多种 第一种: 指定一个向量,给出不同的断点 代码示例: data <- c(rep(1, 10), rep(2, 5), rep(3, 6)) h

R语言boxplot绘图函数

boxplot 用于绘制箱线图,我们都知道boxplot 用于展示一组数据的总体分布,在R语言中,支持两种输入数据的方式 第一种:x , 这个参数指定用于绘制箱线图所用的数据,是一个向量 代码示例: boxplot(1:100) 效果图如下: 第二种, 通多formala 和 data 两个参数指定,适合展示多组数据的分布 代码示例: dataset <- data.frame(value = rep(1:100, times = 2), group = factor(rep(c("A&q

R语言之merge函数案例

R语言的merge函数可以实现类似SQL的有点类似 left join right join 或者类似union的效果. df1 = data.frame(CustomerId=c(1:6),Product=c(rep("Toaster",3),rep("Radio",3))) > df2 = data.frame(CustomerId=c(2,4,6,7),State=c(rep("Alabama",3),rep("Ohio&q

R语言基础绘图

一.可以通过代码或者图形用户界面保存图形,绘图语句夹在开启目标图形设备语句和关闭图形设备的语句之间: pdf("filename.pdf") png("filename.png") jepg("filename.jpg") ........ dev.off() 二.图形参数: 1.通过par()指定参数选项,这种方式设定的参数值除非被再次修改,否则会在绘画结束前一直有效,添加参数no.readonly=TRUE可以生成一个可以修改的当前图形列表参

R语言列表list函数

列表是R语言中的对象,它包含不同类型的元素,比如 - 数字,字符串,向量和另一个列表等.一个列表还可以包含一个矩阵或一个函数作为它的元素.使用list()函数创建列表. 创建一个列表 下面是一个例子来创建一个包含字符串,数字,向量和逻辑值的列表 # Create a list containing strings, numbers, vectors and a logical values. list_data <- list("Red", "Green",