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 函數的幫助文檔