R ggplot grid of rectangles, borders top, bottom, left right individual colors

779 views Asked by At

I am trying to make an image like the below (generated in MATLAB) in R. SOM ANN grid image

I need to use ggplot because I need to add rectangles to the "grid" one by one and set the background of each rectangle according to a matrix that has intensities from 0 to 1. Right now, I have a data frame with all of the intensities for the rectangles (density attribute) and the rectangle borders (tops, lefts, rights, bottoms, top.lefts, etc). A link to my full R script that defines all variables referenced in the dataframe definition below can be found here (edited version): http://pastebin.com/JUUmnTSq.

g <- ggplot()
df <- data.frame(
  x = c(1:somPEs),
  y = c(1:somPEs),
  density = hm2/max(hm2),
  tops = fence_edges[,1],
  bottoms = fence_edges[,2],
  lefts = fence_edges[,3],
  rights = fence_edges[,4],
  top.lefts = fence_diagonals[,1],
  top.rights = fence_diagonals[,2], 
  bottom.lefts = fence_diagonals[,3], 
  bottom.rights = fence_diagonals[,4]
)
ggplot() +
  geom_rect() +     <- the redscaled rectangles 
  geom_segment() +  <-- the grayscaled borders on the rectangles
  geom_line()       <-- the green scatter plots connected by lines

Note the "top.lefts, top.rights" etc are diagonal "pixel" values. Essentially, this is a visual for a Self Organizing Map for neural network unsupervised learning. I basically just don't know how to fill in the geom_*() methods for ggplot, at all, using the dataframe information.

Can anyone help me do this? Ggplot is VERY hard to master and I cannot find out how to get rectangles to add to the same plot using the normal rect(), plot(), segments() functions, etc.

Thanks for any help you can offer!

Update: Here is my progress so far, based on the helpful name dropping of "themes" and "facetting", below. Basically all I want now is the ability to shade in the borders based on the intensities I specify. Any ideas?

enter image description here

# Plot
require(ggplot2)
mxW = max(max(max(w)));
deltax = 1/inputPEs;
hm2 <- as.vector(matrix(hm, byrow=TRUE))  
it=1;
# Reshape w to a 3D matrix of dimension: c(sqrt(somPEs), sqrt(somPEs), inputPEs)
dim(w) <- c(sqrt(somPEs), sqrt(somPEs), inputPEs)
w <- aperm(w, c(2, 1, 3))

df <- data.frame(
  somPEs = c(1:somPEs),      
  xp = do.call(rbind, replicate(6, cbind(matrix(0, nrow=sqrt(somPEs)),
         t(sapply(rep(1,sqrt(somPEs)), function(i) seq(-1+i+deltax,i- 
                                 deltax,deltax)))), simplify=FALSE)),
  yp = sapply(c(1:sqrt(somPEs)), function(i) 0.2+0.8*w[i,1:sqrt(somPEs),]/mxW),
  density = hm2/max(hm2),
  tops = fence_edges[,1],
  bottoms = fence_edges[,2],
  lefts = fence_edges[,3],
  rights = fence_edges[,4],
  top.lefts = fence_diagonals[,1],
  top.rights = fence_diagonals[,2], 
  bottom.lefts = fence_diagonals[,3], 
  bottom.rights = fence_diagonals[,4]
)

ggplot() + geom_rect(data=df,aes(xmin=0,xmax=1,ymin=0,ymax=1,fill=density)) +
geom_line(data=df, aes(x=c(xp.1, xp.2, xp.3, xp.4, xp.5, xp.6), 
      y=c(yp.1, yp.2, yp.3, yp.4, yp.5, yp.6)), colour="green") + 
facet_wrap( ~ somPEs, ncol=sqrt(somPEs)) +
scale_x_continuous(limits = c(0, 1)) + scale_y_continuous(limits = c(0,1)) 
0

There are 0 answers