points()
R语言中的函数用于将一组具有指定形状、大小和颜色的点添加到现有绘图中。
用法: points(x, y, cex, pch, col)
参数:
x, y:坐标向量
cex:点的大小
pch:点的形状
col:点的颜色
样本散点图:
# R program to create a scatter plot
# Creating coordinate vectors
x <- c(1.3, 3.5, 1.4, -3.1, 5.7, 2.4, 3.3, 2.5, 2.3, 1.9, 1.8, 2.3)
y <- c(2.5, 5.8, 2.1, -3, 12, 5, 6.2, 4.8, 4.2, 3.5, 3.7, 5.2)
# Plotting the graph
plot(x, y, cex = 1, pch = 3, xlab ="x", ylab ="y", col ="black")
输出:
此处,为添加点创建了示例散点图。
示例 1:向散点图添加点
# R program to add points to a plot
# Creating coordinate vectors
x <- c(1.3, 3.5, 1.4, -3.1, 5.7, 2.4, 3.3, 2.5, 2.3, 1.9, 1.8, 2.3)
y <- c(2.5, 5.8, 2.1, -3, 12, 5, 6.2, 4.8, 4.2, 3.5, 3.7, 5.2)
plot(x, y, cex = 1, pch = 3, xlab ="x", ylab ="y", col ="black")
# Creating coordinates to be added
x2 <- c(4.2, 1.2, -2.4, -0.3, -1.3, 2.5)
y2 <- c(2.4, 4.3, 1.4, 2.3, -2, 4.5)
# Calling points() function
points(x2, y2, cex = 2, pch = 6, col ="blue")
输出:
示例2:添加不同属性的点
# R program to add points to a plot
# Creating coordinate vectors
x <- c(1.3, 3.5, 1.4, -3.1, 5.7, 2.4, 3.3, 2.5, 2.3, 1.9, 1.8, 2.3)
y <- c(2.5, 5.8, 2.1, -3, 12, 5, 6.2, 4.8, 4.2, 3.5, 3.7, 5.2)
plot(x, y, cex = 1, pch = 3, xlab ="x", ylab ="y", col ="black")
# Creating coordinates to be added
x2 <- c(4.2, 1.2, -2.4, -0.3, -1.3, 2.5)
y2 <- c(2.4, 4.3, 1.4, 2.3, -2, 4.5)
# Calling points() function
points(x2, y2, cex = 2, pch = 6, col ="blue")
# Adding points of different shape and size
x3 <- c(4, 0)
y3 <- c(7, -2)
points(x3, y3, cex = 4, pch = 14, col ="green")
输出:
相关用法
- R语言 lines()用法及代码示例
- R语言 arrows()用法及代码示例
- R语言 sum()用法及代码示例
- R语言 dgeom()用法及代码示例
- R语言 pairs()用法及代码示例
- R语言 axis()用法及代码示例
- R语言 abline()用法及代码示例
- R语言 plot()用法及代码示例
- R语言 segments()用法及代码示例
- R语言 polygon()用法及代码示例
- R语言 is.primitive()用法及代码示例
- R语言 dunif()用法及代码示例
- R语言 lapply()用法及代码示例
注:本文由纯净天空筛选整理自nidhi_biet大神的英文原创作品 Addition of more points to a Plot in R Programming – points() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。