How to obtain density values of the heat map of kernel density distribution ?

#1
Hi all,

So that looks tricky to me since I'm not specialized in statistics and softwares, but I'm hopeful that someone will give me an answer.

I used R to generate a kernel density distribution of puncta within the region outlined in the figure attached. The problem now that I don't know how to identify the puncta density per color (for example, number of puncta detected per hot spots/ red regions). The reason is because I want to attach a heat map to the figure in order to clarify the gradient of puncta clustering (Blue=0, Red= value of highest density)

I used MASS and RColorBrewer packages, and this is the code I used to generate the density distribution map:

>rf <- colorRampPalette(rev(brewer.pal(11,'Spectral')))
>r <- rf(32)
>L1<-read.table("L1.csv",header=TRUE,sep=",")
>L1
>attach(L1)
>plot(Position.X, Position.Y, xlim=c(320,5200), ylim=c(90,5200))
>f2 <- kde2d(Position.X, Position.Y, n=600, lims=c(320,5200,90,5200),
>h = c(width.SJ(Position.X), width.SJ(Position.Y)))
>image(f2, col=r)

Thank you very much in advance :)

Lina
 
Last edited:

bugman

Super Moderator
#2
Try the image.plot function in the "fields" package

then Contour(name of image,add=T)


Code:
library(fields)
image.plot(f2)
contour(f2, add = TRUE)
 
#3
Hey bugman,

Thank you for your quick reply, I really appreciate it.
I used your code and it seems that it is working and showing values for the heat map as shown in the figure attached (upside down this time). But the thing I don't understand what these values mean? I don't think they represent number of puncta per area, which is the number I'm looking for. Do you have any idea how can I get that number?

Cheers,

Lina
Try the image.plot function in the "fields" package

then Contour(name of image,add=T)


Code:
library(fields)
image.plot(f2)
contour(f2, add = TRUE)
 

bugman

Super Moderator
#6
Yes. Thats what I mean. Set them to some meaningful limit (within the bounds of your data range) and the legend should make more sense.
 
#7
Hey Bugman,

So I'm trying the contours as you advised, I'm stuck with this warning message that is shown in the picture attached, I think there is a problem with the Var value, but I don't know what is it. I'm trying to follow this code below which I found online.
The thing is, will this contour code actually gives me the value of dots/puncta detected within each color? Is that what the values represent, because the figure that I found (in more than one website) looks like concentric circles rather than density distribution!

The code I'm following:

x <- 1:50
y <- 1:70
z <- matrix(expand.grid(x,y)$Var1^2 + expand.grid(x,y)$Var2^2,50,70)

# plain
contour(x,y,z)

# adjusting levels
mylevels <- seq(0,7500,500)
contour(x,y,z,levels=mylevels,xaxs='i',yaxs='i')

# filled contours
filled.contour(x,y,z,color.palette=heat.colors)
filled.contour(x,y,z,col=grey(seq(0,1,length=length(mylevels))))
# add this to line above and see bad match
contour(x,y,z,levels=mylevels,add=T)

Thank you

Lina

Yes. Thats what I mean. Set them to some meaningful limit (within the bounds of your data range) and the legend should make more sense.