contour map and density map

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

contour map and density map

saccente
Hi there community,
I'm not a specialist with Bio7 and especially with R.

I'm trying to do 2 things:
1) to get a densitymap (heatmap) with X,Y (trues coordinates from imageJ analyze particles function)
2) to plot the same above coordinates X,Y and correlate them with Z (area of the particles) in a graph such a contour map.
Does someone has any strings in R for that?
This would be of great help for me, if i get famous with my project i'll share this :)
Thank you
Sacha
Reply | Threaded
Open this post in threaded view
|

Re: contour map and density map

Marcel
Hello Sacha,

to become famous I would recommend you the 'spatstat' package which is best suited to do this kind of plotting and analysis.

You have to install the package and then you can create heatmaps, etc.

To plot the x,y coordinates transfer the particle analysis with the selected centroid and area values.

Select the values in the ImageJ menu: 'Analyze->Set Measurements...'

See video: https://www.youtube.com/watch?v=gUQRqfh7ZNc

After the transfer (variables ImageWidth and ImageHeight will be available after transfer!) you can plot the points with:

library(spatstat)
X<- ppp(Particles$X, Particles$Y, c(0,ImageWidth), c(0,ImageHeight))
plot.ppp(X,axes=TRUE)

In the next example I plot the coordinates like in ImageJ (0,0 coordinates in the upper left) and with custom axes.

library(spatstat)
xrange <- c(0,ImageWidth)
yrange <- c(ImageHeight,0)
X<- ppp(Particles$X, Particles$Y, c(0,ImageWidth), c(0,ImageHeight))
plot(x = xrange, y = yrange,xlim=c(1,ImageWidth),ylim=c(ImageHeight,1), type = "n", main = "", asp = 1, axes = F, xlab = "x", ylab = "y")
plot(X,axes=TRUE,xlim=c(1,ImageWidth),ylim=c(ImageHeight,1),add = T)
axis(1)
axis(2, las = 2)

For a density map you can plot:

plot(density(X))
plot(X,add=TRUE)

And here you find an example of the mark correlation function:

http://www.inside-r.org/packages/cran/spatstat/docs/markcorr

For a marked point pattern (in this example the area):

X<- ppp(Particles$X, Particles$Y, c(0,ImageWidth), c(0,ImageHeight),marks=Particles$Area)
plot(density(X, weights=Particles$Area))

Please consult the spatstat documentation for more details.

(type 'spatstat' in the texfield of the R-Shell view and then press the ? action button)




Reply | Threaded
Open this post in threaded view
|

Re: contour map and density map

Marcel
In reply to this post by saccente
And you can Plot a contour Plot with:

contour(density(X, weights=Particles$Area))
Reply | Threaded
Open this post in threaded view
|

Re: contour map and density map

saccente
Thank you enormously Marcel,
after some trials I managed to get nice plots and learn basics. I'm happy to see how Bio7 is taking full advantage of R and Spatstat.

The last thing I couldn't get by myself is how to display a colored contour with the average of the X$mark within each level (something similar as the "RainForest" example but with the average of the diameter of the trees instead of their number).
I know this is a bit more complicated, but any advise is welcome!

Best regards
Sacha
Reply | Threaded
Open this post in threaded view
|

Re: contour map and density map

Marcel
Dear Sacha,

it might be useful to tesselate the point pattern according to the level into a specific window size and the iterate over the single patterns to generate from the mean a factored heatmap.

Eventually the use of another rpackage could be more useful, too, e.g., the 'sp' and 'rgeos' package to perform a spatial operation to extract the leveled point pattern as a 'SpatialPolygonsDataFrame'. Then you can visualize the mean valued SpatialPolygonsDataframe's.

However this is just an idea.