R语言 为图表添加标题 – title() 函数
R语言中的 title() 函数用于向图表添加主标题和轴标题。这个函数也可以用来修改现有的标题。
语法:
title(main = NULL, sub = NULL, xlab = NULL, ylab = NULL, ...)
参数:
main: 图形的主标题
sub: 定义字幕
返回: 添加标题后的图形
例1 :
# R program to add title to a Graph
# Specifying axis values
x<-1:5; y<-x*x
# Creating a plot
plot(x, y, main = "", xlab = "", ylab = "",
col.axis = "darkgreen")
# Calling title() function
title(main = "Graph ", sub = "Geeksforgeeks article",
xlab = "X axis", ylab = "Y axis",
cex.main = 4, font.main = 3, col.main = "darkgreen",
cex.sub = 2, font.sub = 3, col.sub = "darkgreen",
col.lab ="black"
)
输出
在上面的例子中,主标题和副标题被添加到绘图中。可用于改变字体大小的参数如下。
- cex.main: 主标题的大小
- cex.lab: 轴标题的文字大小
- cex.sub: 副标题的文字大小
例2 :
barplot(c(1, 10) )
title(main = "PLOT ", sub = "Geeksforgeeks article",
xlab = "X axis", ylab = "Y axis",
# Change the colors
col.main="darkgreen", col.lab="black", col.sub="darkgreen",
# Titles in italic and bold
font.main = 4, font.lab = 4, font.sub = 3,
# Change font size
cex.main = 3, cex.lab = 1.7, cex.sub = 2
)
输出