Skip to content

Visualize Sparse Matrix.

4 messages · FRANCISCO XAVIER SUMBA TORAL, Jim Lemon

#
Hi,

First of all, sorry for my question it could be so basic for a common user in R, but I am starting with this new environment.

I have done a clustering job and I would like to visualize my vectors. I have a matrix of TF-IDF weights of 4602 x 1817. I store the values in a CSV file. How can I visualize my vectors in a 2D-space?

After that, I execute a clustering algorithm and I got a label for each cluster. How can I visualize my vectors resulting base on a color or figure for each cluster? 

This is the code that I am having trying to accomplish my graphs:

data <- read.csv(pathFile,header = FALSE, sep = ",?)
dMatrix <- matrix(unlist(data), ncol = 4602, byrow = TRUE) # Use a matrix to use melt.
# Graph my data
ggplot(melt(dMatrix), aes(Var1,Var2, fill=value)) + geom_raster() + scale_fill_gradient2(low='red', high=?black', mid=?white') + theme_bw() + xlab("x1") + ylab("x2")


Cheers.
#
Hi Francisco,
I tried this just to see if it would work. It did, after a while.

wtmat<-matrix(rnorm(4602*1817),nrow=4602)
library(plotrix)
x11(width=5,height=13)
color2D.matplot(wtmat,c(1,1,0),c(0,1,0),0,border=FALSE)

Jim

On Fri, Jun 10, 2016 at 8:27 AM, FRANCISCO XAVIER SUMBA TORAL
<xavier.sumba93 at ucuenca.ec> wrote:
#
Hi Jim,

Thanks for your answer. 

I try your code example, but it is basically the same that I had it. I want to visualise my matrix something like this image: 




With the graphics that I already have is difficult to visualise my data. I am getting this results:

1) With my first code, I got this:



2) With Jim?s code. I got this: 



Ho can I make my graphs more observable as in the first figure? My graphs shows points as if my screen was dirty. 

Cheers.
#
Hi Francisco,
Your example plot shows me what you want to do (I think). I'm guessing that
you want to display the values in your matrix that are NOT zero or NA,
either colored in some way, or just in one color as the example. The
following example shows how to do both of these:

# wtmat<-matrix(rnorm(4602*1817),nrow=4602)
# use a smaller matrix to illustrate the principle
wtmat<-matrix(rnorm(46*18),nrow=46)
# make it "sparse" by taking out all small values
# in your case this may be changing all zero values to NS
wtmat[abs(wtmat)<1]<-NA
library(plotrix)
x11(width=5,height=13)
# display all values in the matrix
# colored as red->white (negative values), white (NA)
# and white->black (positive values)
color2D.matplot(wtmat,c(1,1,0),c(0,1,0),c(0,1,0),border=FALSE)
# now do a plot just showing values that are not NA
color2D.matplot(abs(wtmat),extremes=c(4,4),border=FALSE)

My original example also looked "dirty", albeit colorful, because there
were so many rectangles on it. With a PDF plot about 500mm high you can see
the individual rectangles in a matrix plot of your original dimensions.

Jim


On Sat, Jun 11, 2016 at 3:29 AM, FRANCISCO XAVIER SUMBA TORAL <
xavier.sumba93 at ucuenca.ec> wrote: