R matrix plot with color threshold and grid

my matrix consists of values โ€‹โ€‹from 0 to 100 and has dimensions of 100 x 100. I basically want to build this matrix, but the color is all values โ€‹โ€‹above 50, for example. red and lower for example. blue. Also, I would like to add a nice gray grid, as they do it here with ggplot:

http://learnr.wordpress.com/2009/07/15/ggplot2-version-of-figures-in-lattice-multivariate-data-visualization-with-r-part-5/

I am wondering what is the easiest way to achieve this? I'm not sure if I want to try ggplot as it looks rather complicated from what I have seen so far. Is there any other simple graphing function for such a task?

+4
source share
3 answers

I am not 100% sure if your data is in the matrix and you need a graph like a heat map. Or if it is in some other form and you want the scatter chart to be similar to the ones you are referencing. I just assumed that your data is described and you want to get a heat map. I assume this is something like:

x=abs(rnorm(100*100,50,25)) x=matrix(x,nrow=100) 

So, I would modify the data so that it looks like xy coordinates with:

 require(reshape2) require(ggplot2) x1=melt(x) names(x1)=c("x","y","color") 

Then I would make my circumcision a factor:

 x1$color=factor(x1$color>50) levels(x1$color)=c("lessthan50","more than 50") 

Then call ggplot with:

 qplot(x, y, fill=color, data=x1,geom='tile') 

enter image description here

+4
source

In the base chart, this is simple:

 image(x, col=c("red","blue")[1+(x>50)] ) 

To add grid usage:

 grid(nx=100, ny=100, lty=1) 
+5
source

You can do this quite simply with levelplot,

 x <- abs(runif(100*100,0, 100)) x <- matrix(x,nrow=100) levelplot(x, cuts=1, col.regions=c("red", "blue")) 
0
source

All Articles