【问题标题】:Legend not showing from K-Means clustering图例未从 K-Means 聚类中显示
【发布时间】:2012-10-17 14:58:03
【问题描述】:

我使用 K-means 聚类对一些值进行了聚类。我绘制了三张图,我想要第三张图的图例。我使用lengend()函数绘制了一个图例,但是图例中没有出现(见图)并且没有报错。

library(cluster) 
library(MASS)

par(mfrow=c(1,3))
par(oma=c(5,0,3,0))

dat <- read.table("representative_for_r.txt", header=TRUE)


data.p <- as.matrix(dat$Average_RMSD)

# Determine number of clusters
wss <- (nrow(data.p)-1)*sum(apply(data.p,2,var))
for (i in 2:15) wss[i] <- sum(kmeans(data.p,centers=i)$withinss)
plot(1:15, wss, type="b", xlab="Number of Clusters",ylab="Within groups sum of squares")



# K-Means Cluster Analysis
fit <- kmeans(data.p, 6) # 6 cluster solution

# get cluster means 
aggregate(data.p,by=list(fit$cluster),FUN=mean)

# append cluster assignment
data.p <- data.frame(data.p, fit$cluster)


# PLot Clusters
clusplot(data.p, fit$cluster, color=TRUE, shade=TRUE, labels=2, lines=0)

data.p <- data.frame(dat$PDB, data.p)
#print(data.p)

plot(data.p[,2],col=data.p$fit.cluster) # takes the RMSD column of data.p(by indexing) then colours the points defined by the clustering
par(xpd=NA)
legend(0,0, c("Group 1","Group 2", "Group 3", "Group 4", "Group 5", "Group 6", "Group 7"), pch=1, col=1:7)

write.matrix(data.p, file = "kmeans_output.txt", sep = " ")

输出如下:

我已尝试使用以下方法更改 X 和 Y 值:

200,200
-200,200
200,-200
-200,-200

【问题讨论】:

    标签: r legend


    【解决方案1】:

    试试:

    legend("bottomleft", legend = paste("Group", 1:7), pch=1, col=1:7)
    

    注意y 轴上的刻度。您传递给legend() 的第二个坐标必须位于绘图范围内。 0-200200 都(方式)超出了图 3 的限制。

    如果您不想猜测这些事情,使用字符来定义位置会更加直观。有关可以提供的字符位置的详细信息,请参阅?legend

    【讨论】:

      猜你喜欢
      • 2013-08-08
      • 2015-04-11
      • 1970-01-01
      • 2011-08-13
      • 2013-02-14
      • 2018-01-14
      • 2018-11-11
      • 2013-01-11
      • 2016-02-01
      相关资源
      最近更新 更多