R基函数points()可以复制

时间:2018-11-23 05:51:30

标签: r

我一直试图弄清楚为什么我只能看到标有 points()的两个图之一。

set.seed(0)
SSE <- rep(0, 10)
for (i in 1:10){
  km.out <- kmeans(x = iris[,1:4], centers = 3, nstart = i)
  SSE[i] <- km.out$tot.withins
}
plot(1:10, SSE, xlab = "k", ylab = "SSE", type = "b")
km.out$cluster

plot(iris[,1:2], col=(km.out$cluster+1), main="Best solution k = 3", xlab="x1", ylab="x2")
points(km.out$centers, col = 1:2, pch = 8, cex = 2)

plot(iris[,3:4], col=(km.out$cluster+1), main="Best solution k = 3", xlab="x1", ylab="x2")
points(km.out$centers, col = c3:4, pch = 8, cex = 2) # didn't work, not sure why
  

第一个情节有重点。

First plot

  

第二个图,没有绘制点。

Second plot

有人知道我在做什么错吗?

1 个答案:

答案 0 :(得分:2)

这些点超出了图形范围。像这样增加ylim:

 plot(iris[,3:4], col=(km.out$cluster+1), main="Best solution k = 3", xlab="x1", ylab="x2", ylim=c(0,4))
 points(km.out$centers, col = 3:4, pch = 8, cex = 2)

您会看到的一切。

或自行调整点:

 points(km.out$centers[, 3:4], col = 3:4, pch = 8, cex = 2)

enter image description here