-1

How can I make ggplot work with this data I tried normal plotting and it works just fine, but I want better visualization when I use ggplot, it gives me the above error, How do I fix this ?

This is an implementation of spectral clustering algorithm. and the code works well and the data is classified correctly. I just need to show this now .

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)
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
Muhammed Eltabakh
  • 375
  • 1
  • 10
  • 24
  • 3
    convert `matrix` to `data.frame` – Sandipan Dey Feb 06 '17 at 17:41
  • when I convert input data to a data frame it gives me an empty plot ! what am I doing wrong ? – Muhammed Eltabakh Feb 06 '17 at 18:12
  • What exactly did you expect it to produce? you're not actually telling it to plot anything. `ggplot()` accepts a data frame (as Sandipan said) and then you have to tell it how to map aestetics to plot elements like x and y values, or color. `col = cl$cluster` isn't ggplot2 style. you can map color to a column name without the `$` though. But more importantly, you never declared any geom (e.g. point, bar, line, etc). There's lots of good documentation out there to help you along, though! – Matt74 Feb 06 '17 at 20:34
  • Thank you I found the answer. it my first experience with plot and it was fun. Thanks for your help again – Muhammed Eltabakh Feb 06 '17 at 21:03
  • there are quite a few things that look wrong in your code, but i) it's not reproducible (no data); ii) it should be a _minimal example_ illustrating _one_ specific problem. – baptiste Feb 07 '17 at 03:02

3 Answers3

3

Just convert class matrix to data frame and do make sure that you store that data frame in another object.

dataFrame<-data.frame(classMatrix)

Now, use ggplot on this object dataFrame.

miPlodder
  • 795
  • 8
  • 18
0

there isn't a matrix method for the ggplot generic function, presumably because a matrix may be interpreted in different ways for plotting purposes (e.g. should one consider the first column to be of particular importance when converting to long format?). You can easily define your own method however (technically one should probably write a fortify method),

m <- cbind(1:10, sin(1:10), cos(1:10))
ggplot.matrix <- function (data = NULL, mapping = aes(), ..., environment = parent.frame()) 
{
    d <- reshape2::melt(as.data.frame(data), 1)
    ggplot(d, mapping, environment = environment)
}

ggplot(m, aes(V1,value, colour=variable)) + geom_line()

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294
-1

I converted input_data into a data frame and solved it using

ggplot(input_data1,aes(x=input_data1$x1, y=input_data1$x2, colour = cl$cluster))+ geom_point(shape=19)
Muhammed Eltabakh
  • 375
  • 1
  • 10
  • 24
  • although this solution might produce a good plot for you, it is better practice to NOT use the `$` notation in ggplot2; just use the raw column names – Matt74 Feb 06 '17 at 21:54
  • I can see that now, I'm having troubles because of that in last 10 minutes. any Ideas how can I change colors of my data in ggplot ! – Muhammed Eltabakh Feb 06 '17 at 22:13
  • it's not just _better practice to avoid it_ – using `$` in aes is **always** a mistake. – baptiste Feb 07 '17 at 02:59