Re: [R] re-arranging data to create an image (or heatmap)
I have now advanced to image.plot from the "fields" package, which gives a nice color scalebar: image.plot(dat,col=imagecolsmat,xlab="time",ylab="abs") All that is left now is to change the scale on the x and y axis from 0 to 1 to the actual values. I do not understand why it does not read the proper values from my data matrix. Here is the first part of it: dat[1:10,1:10] abs time 5 6 7 8 9 10 11 12 13 14 0.248175182 1 0 1 0 0 0 0 0 0 0 0.372262774 1 0 1 0 0 0 0 0 0 0 0.496350365 1 0 1 0 0 0 0 0 0 0 0.620437956 2 0 1 0 0 0 0 0 0 0 0.744525547 2 1 0 0 0 0 0 0 0 0 0.868613139 2 1 0 0 0 0 0 0 0 0 0.99270073 0 2 1 0 0 0 0 0 0 0 1.116788321 0 3 0 0 0 0 0 0 0 0 1.240875912 0 1 2 0 0 0 0 0 0 0 1.364963504 0 0 1 3 1 0 0 0 0 0 It seems as if the first row and column are disregarded and the data is rescaled to fit between 0 and 1. It seems I'm so close, but I can't seem to figure this one out... This is what I get so far: http://r.789695.n4.nabble.com/file/n3341466/test.jpg -- View this message in context: http://r.789695.n4.nabble.com/re-arranging-data-to-create-an-image-or-heatmap-tp3327986p3341466.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] re-arranging data to create an image (or heatmap)
It turned out that rearranging the data was indeed the key to get the image I want. The way I do it now is this: tt <- read.csv(file="file.csv", header=T, sep=",", dec=".", stringsAsFactors=F) names(tt) <- c('time','abs') dat <- with(tt, table(time, abs)) image(dat,col=rainbow(256)) I'm now modifying the rainbow color palette to match my needs. Also, the X-axis and Y-axis display values between 0 and 1, so I want to change them to show the actual time and abs values. -- View this message in context: http://r.789695.n4.nabble.com/re-arranging-data-to-create-an-image-or-heatmap-tp3327986p3339498.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [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: marchy...@hotmail.com > To: s.pier...@gmail.com; r-help@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.pier...@gmail.com > > To: r-help@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@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. > __ R-help@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.
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. -- 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@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.
Re: [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 = "#FF20") - 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.0971428624 [2,] 0.1942857124 [3,] 0.1942857124 [4,] 0.2914285724 [5,] 0.3885714323 [6,] 0.3885714322 [7,] 0.4857142923 [8,] 0.5828571421 [9,] 0.5828571421 [10,] 0.680023 [11,] 0.680025 [12,] 0.680023 [13,] 0.7771428623 [14,] 0.7771428623 [15,] 0.8742857121 [16,] 0.8742857120 [17,] 0.8742857122 [18,] 1.0685714323 [19,] 1.0685714325 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: cbelei...@units.it __ R-help@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.
[R] re-arranging data to create an image (or heatmap)
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.0971428624 [2,] 0.1942857124 [3,] 0.1942857124 [4,] 0.2914285724 [5,] 0.3885714323 [6,] 0.3885714322 [7,] 0.4857142923 [8,] 0.5828571421 [9,] 0.5828571421 [10,] 0.680023 [11,] 0.680025 [12,] 0.680023 [13,] 0.7771428623 [14,] 0.7771428623 [15,] 0.8742857121 [16,] 0.8742857120 [17,] 0.8742857122 [18,] 1.0685714323 [19,] 1.0685714325 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. __ R-help@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.