Let me start by introducing myself as a biologist with only a little knowledge about programming in matlab and R. In the past I have succesfully created my figures in matlab using the hist3d command, but I have not access to matlab right now and would like to switch to R. I have used the plot command to create a figure of my data and it does almost what I want it to do. My data matrix looks like this (these are the first few lines from it, copied from R console): Time Abs [1,] 0.09714286 24 [2,] 0.19428571 24 [3,] 0.19428571 24 [4,] 0.29142857 24 [5,] 0.38857143 23 [6,] 0.38857143 22 [7,] 0.48571429 23 [8,] 0.58285714 21 [9,] 0.58285714 21 [10,] 0.68000000 23 [11,] 0.68000000 25 [12,] 0.68000000 23 [13,] 0.77714286 23 [14,] 0.77714286 23 [15,] 0.87428571 21 [16,] 0.87428571 20 [17,] 0.87428571 22 [18,] 1.06857143 23 [19,] 1.06857143 25 The example shows that some of the plotted points appear more than once. I would like to use a heatmap to show that these points have more weight, but I have difficulty arranging the data to be plotted correctly using the image() or heatmap() command. So what I would want to do is to get the same figure as when I use the plot command, but have colors representing the weight of the plotted points (wether they occur once, twice or multiple times). I have tried searching this forum and also used google, but I seem to keep going in circles. I think that the image() command fits my needs, but that my input data is not in the correct format. Attached I have an image example from R and an image example from matlab. This is how far I got using R: http://r.789695.n4.nabble.com/file/n3327986/example_R.jpg This is the result I am aiming for: http://r.789695.n4.nabble.com/file/n3327986/example_matlab.jpg -- View this message in context: http://r.789695.n4.nabble.com/re-arranging-data-to-create-an-image-or-heatmap-tp3327986p3327986.html Sent from the R help mailing list archive at Nabble.com.
Claudia Beleites
2011-Feb-28 14:46 UTC
[R] re-arranging data to create an image (or heatmap)
Pierz, - easy approximation: use coulours with alpha value for plotting points > x <- runif (1000) > y <- runif (1000) * x + x > plot (x, y) > plot (x, y, pch = 20, col = "#FF000020") - more sophisticated: have a look at hexbin (package hexbin), levelplot (package lattice), package ggplot2 with stat_sum, stat_binhex, or stat_density2d (e.g. http://had.co.nz/ggplot2/stat_sum.html) HTH Claudia On 02/28/2011 03:21 PM, pierz wrote:> Let me start by introducing myself as a biologist with only a little > knowledge about programming in matlab and R. In the past I have succesfully > created my figures in matlab using the hist3d command, but I have not access > to matlab right now and would like to switch to R. > > I have used the plot command to create a figure of my data and it does > almost what I want it to do. > > My data matrix looks like this (these are the first few lines from it, > copied from R console): > > Time Abs > [1,] 0.09714286 24 > [2,] 0.19428571 24 > [3,] 0.19428571 24 > [4,] 0.29142857 24 > [5,] 0.38857143 23 > [6,] 0.38857143 22 > [7,] 0.48571429 23 > [8,] 0.58285714 21 > [9,] 0.58285714 21 > [10,] 0.68000000 23 > [11,] 0.68000000 25 > [12,] 0.68000000 23 > [13,] 0.77714286 23 > [14,] 0.77714286 23 > [15,] 0.87428571 21 > [16,] 0.87428571 20 > [17,] 0.87428571 22 > [18,] 1.06857143 23 > [19,] 1.06857143 25 > > The example shows that some of the plotted points appear more than once. I > would like to use a heatmap to show that these points have more weight, but > I have difficulty arranging the data to be plotted correctly using the > image() or heatmap() command. > > So what I would want to do is to get the same figure as when I use the plot > command, but have colors representing the weight of the plotted points > (wether they occur once, twice or multiple times). > > I have tried searching this forum and also used google, but I seem to keep > going in circles. I think that the image() command fits my needs, but that > my input data is not in the correct format. > > Attached I have an image example from R and an image example from matlab. > This is how far I got using R: > http://r.789695.n4.nabble.com/file/n3327986/example_R.jpg > > This is the result I am aiming for: > http://r.789695.n4.nabble.com/file/n3327986/example_matlab.jpg > >-- Claudia Beleites Dipartimento dei Materiali e delle Risorse Naturali Universit? degli Studi di Trieste Via Alfonso Valerio 6/a I-34127 Trieste phone: +39 0 40 5 58-37 68 email: cbeleites at units.it
Thank you for your quick reply. The change in transparency makes a big difference. I will have a look at the other packages to see if i can do multiple colors. -- View this message in context: http://r.789695.n4.nabble.com/re-arranging-data-to-create-an-image-or-heatmap-tp3327986p3328227.html Sent from the R help mailing list archive at Nabble.com.
Mike Marchywka
2011-Feb-28 18:18 UTC
[R] re-arranging data to create an image (or heatmap)
ok, third try. This is a repost of a message never made it past mod as I cancelled while it was waiting to clean up some code to at least make it close to working. I think this does more or less what you want,> library("VecStatGraphs2D") > > DrawDensityMap(x1,x2,PaintPoint=TRUE)but if not see the function below that while clumsy does seem to color about as I would expect given the density function I created. again, "for illustration only" don't use for life support system or nuclear power plants etc. ----------------------------------------> From: marchywka at hotmail.com > To: s.piersma at gmail.com; r-help at r-project.org > Subject: RE: [R] re-arranging data to create an image (or heatmap) > Date: Mon, 28 Feb 2011 11:47:30 -0500 > > > > > > > > > > > ---------------------------------------- > > Date: Mon, 28 Feb 2011 08:23:41 -0800 > > From: s.piersma at gmail.com > > To: r-help at r-project.org > > Subject: Re: [R] re-arranging data to create an image (or heatmap) > > > > Thank you for your quick reply. The change in transparency makes a big > > difference. I will have a look at the other packages to see if i can do > > multiple colors. > > This is a problem has has been bugging me for a while now but I hadn't searched > for a solution. However, it does seem that google on 'R "point density" CRAN' > turns up an intersting packaged called "VecStatGraph2D." I used the sample in the > docs and it doesn't color the points but maybe it will work for you, I plan on using > it soon, > > x1=rnorm(1000) > x2=rnorm(1000) > plot(x2,x1) > > library("VecStatGraphs2D") > > DrawDensityMap(x1,x2,PaintPoint=TRUE) > > history(500) > > > However, if you can find a density measure of you own, you an play with this. > Perhaps an R person can remove the loops and other kluges but generally you just > want to make a color table and pick from that for your plotting. For example, > assume that each point creates a 1/r^2 density and then add up the density at > each point ( would appreciate a cleaner way to do this with or without snide comments > LOL), > >mydensity<-function(x1,x2) { len=length(x1); z<-1:len; for ( y in 1:len ) { nx=c(1:(y-1),(y+1):len); if ( y==1) {nx=2:len;} if (y==len) {nx=1:(len-1); } z[y]=sum(1.0/(.0001+(x1[nx]-x1[y])*(x1[nx]-x1[y])+(x2[nx]-x2[y])*(x2[nx]-x2[y]))); #for } z=sqrt(z); z=sqrt(z); print(max(z)); print(min(z)); z=z-min(z); z=10*z/max(z); hist(z); color=rainbow(10); #color=heat.colors(10); tmap=color[floor(z)+1]; scatterplot3d(x1,x2,z,color=tmap); plot(x2,x1,col=tmap) #library("VecStatGraphs2D") #DrawDensityMap(x1,x2,PaintPoint=TRUE) #color=terrain.colors(26) #color=heat.colors(26) }> > > > > > > > > > -- > > View this message in context: http://r.789695.n4.nabble.com/re-arranging-data-to-create-an-image-or-heatmap-tp3327986p3328227.html > > Sent from the R help mailing list archive at Nabble.com. > > > > ______________________________________________ > > R-help at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. >