在R编程中将轴添加到绘图中-axis()函数
R语言中的axis()
函数是将轴添加到绘图中。它占据了要绘制轴作为参数的绘图一侧。
Syntax:
axis(side, at=NULL, labels=TRUE)
Parameters:
side: It defines the side of the plot the axis is to be drawn on possible values such as below, left, above, and right.
at: Point to draw tick marks
labels: Specifies texts for tick-mark labels.
示例 1:
# R program to draw axis in a plot
x <- 1:5; y = x * x
plot(x, y, axes = FALSE)
# Calling the axis() function
axis(side = 1, at = 1:5, labels = LETTERS[1:5])
axis(3)
输出:
在上面的示例中,将直线虚线添加到连接数字和字母值的绘图中,绘图轴绘制在绘图的顶部和底部。
示例 2:通过绘制框添加轴的另一个示例
# R program to draw axis to a plot
x<-1:5; y = x * x
plot(x, y, axes = FALSE)
axis(side=1, at = 1:5, labels = LETTERS[1:5])
axis(3)
#- To make it look like "usual" plot
box()
输出:
在这里,使用box()函数围绕轴绘制一个框,使其看起来像通常的绘图。
示例 3:
# R program to draw axis to a plot
x<-1:4; y=x*x
plot(x, y, pch = 18, col = "darkgreen", type = "b",
frame = FALSE, xaxt = "n")
# Remove x axis
axis(1, 1:4, LETTERS[1:4], col.axis="blue")
axis(3, col = "darkgreen", lty = 2, lwd = 4)
axis(4, col = "black", col.axis = "blue", lwd = 4)
输出:
在这里,绘制了不同类型的轴,以实现线宽(lwd)和线型(lty)的修改。