어떻게하면 ggplot
을이 데이터와 함께 사용할 수 있습니까? 일반 플로팅을 시도했지만 정상적으로 작동하지만 ggplot
을 사용할 때 더 나은 시각화가 필요합니다. 위 오류가 발생합니다. 어떻게 해결합니까?오류 : ggplot2는 클래스 매트릭스의 데이터를 처리하는 방법을 모릅니다.
이것은 스펙트럼 클러스터링 알고리즘의 구현입니다. 코드가 잘 작동하고 데이터가 올바르게 분류됩니다. 이제 이것을 보여줄뿐입니다.
library(ggplot2)
input_data <- as.matrix(read.table("SpectData.txt"))
colnames(input_data) <- c("x1", "x2")
#1- Define Weights matrix
W <- matrix(0,nrow = nrow(input_data),ncol = nrow(input_data))
#2- Define Degree Matrix
D <- matrix(0,nrow = nrow(input_data),ncol = nrow(input_data))
calculateWeight <- function(x1,x2,sigma) {
result <- exp(- (norm(x2-x1,type = "2"))^2/ (2*sigma^2))
result
}
calcWieghtMatrix <- function(sigma) {
for(i in 1: nrow(W)){
for(j in 1: nrow(W)){
if(i == j){
next
}
if(W[i,j] != 0){
next
}
W[i,j] <<- calculateWeight(input_data[i,],input_data[j,],sigma)
W[j,i] <<- W[i,j]
}
}
}
calcDegreeMatrix <- function() {
for(i in 1:nrow(input_data)){
D[i,i] <<- sum(W[i,])
}
}
executeSpectralClustring <- function (sigma,numberOfClusters){
calcWieghtMatrix(sigma)
calcDegreeMatrix()
L <<- D - W
eigenDecompostion <- eigen(L,symmetric = FALSE)
index <- ncol(eigenDecompostion$vectors)-1
eigenVector <- eigenDecompostion$vectors[,index]
cl <- kmeans(eigenVector,numberOfClusters)
ggplot(input_data,col = cl$cluster)
}
executeSpectralClustring(0.01,2)
'매트릭스'를'data.frame'으로 변환합니다. –
입력 데이터를 데이터 프레임으로 변환하면 빈 플롯이 생깁니다! 내가 도대체 뭘 잘못하고있는 겁니까 ? –
정확히 무엇을 생산할 것으로 기대 했습니까? 당신은 실제로 무엇이든 음모를 꾸미는 것을 말하지 않고 있습니다. 'ggplot()'은 데이터 프레임을 받아들입니다 (Sandipan이 말했듯이). 그러면 aestetics를 x, y 값 또는 색상과 같은 플롯 요소에 매핑하는 방법을 말해야합니다. 'col = cl $ cluster'는 ggplot2 스타일이 아닙니다. 당신은'$ '없이 색상을 칼럼 이름에 매핑 할 수있다. 하지만 더 중요한 것은 어떤 기하도 (예 : 점, 막대, 선 등)를 선언하지 않은 것입니다. 그래도 거기에 도움이되는 훌륭한 문서가 많이 있습니다! – Matt74