2012-10-17 77 views
0

我已經使用K均值聚類對一些值進行了聚類。我繪製三張圖,我想爲第三張圖創建一個圖例。我使用lengend()函數繪製圖例,但圖例中未顯示圖例(請參見圖片),並且未報告任何錯誤。圖例未顯示來自K均值聚類

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 = " ") 

輸出看起來像:

enter image description here

我曾嘗試與改變X和Y的值如下:

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

回答

1

嘗試:

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

請注意y軸上的刻度。您傳遞給legend()的第二個座標必須位於該圖的範圍內。 0以及-200200都是(超出圖3的限制)。

如果您不想猜這些東西,則使用字符來定義放置位置會更加直觀。有關可提供的角色位置的詳細信息,請參閱?legend