The Spotfire Community is moving to TIBCOmmunity and this forum location has closed. During the transition, you can still search the old forums but posting has been disabled. We encourage you to pick up the discussion at the new Spotfire community on TIBCOmmunity.
calculate density of points in a scatter plot - TIBCO Spotfire Community

calculate density of points in a scatter plot

Last post Tue, Oct 23 2012 8:31 PM by jminardi. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • Wed, Feb 23 2011 2:03 AM

    calculate density of points in a scatter plot

    I am working with a 2D scatter plot that has a region where the points are in high density.  I would like to convert such density into a value so I can select points of given density ranges.  This is similar to 3d binning.  Currently I can get a wireframe plot using origin lab software but unfortunately this package gives me the bins in a matrix table so I cannot use it to select specific points.  Can someone help me with this?

    Many thanks


  • Mon, Feb 28 2011 5:07 PM In reply to

    Re: calculate density of points in a scatter plot

    Hi npagratis, Not sure exactly the use case, but the following may help. A naive approach might be to calculate the number of "close" neighbors to a point as a measure of the density there. The code below takes this approach and plots. The density, by construction, is an integer z, so you could make selections based on it. 

    x <- runif(100) ^ 2
    y <- 1 - (runif(100)) ^ 2 - (runif(100) ^ 3)

    # Calculate the number of close neighbors.
    window.size <- .1
    z <- unlist(lapply(1:length(x),
                       function(n, x, y, window.size) {
                         x0 <- x[n]
                         y0 <- y[n]
                         z <- sum(sqrt((x - x0)^2 + (y - y0)^2) < window.size)
                       x=x, y=y, window.size=window.size))

    par(mfcol=c(1, 2))
    plot(x, y, type="n", main="data colored by density")
    points(x, y, pch=8, col=z)
    key(text=list(sort(unique(z))), rect=list(col=sort(unique(z))))

    plot(x, y, type="n", main="high-density observations")
    points(x[z > 5], y[z > 5], pch=8, col="red")

    Filed under: ,
  • Mon, Oct 22 2012 12:23 AM In reply to

    • ltoledo
    • Not Ranked
    • Joined on Mon, Oct 22 2012
    • Posts 1

    Re: calculate density of points in a scatter plot

    Hi jminardi, I just found your answer to npagratis question, which is actually my problem as well (for every single plot I deal with). I am a beginner user of Spotfire and although I can get a glance of what the script does, I don┬┤t even know how should I run it in the software. Could you give me just a couple of hints on how to do that? Thanks!
  • Tue, Oct 23 2012 8:31 PM In reply to

    Re: calculate density of points in a scatter plot

    Hi ltoledo, (TIBCO Spotfire Statistics Services is needed to do this.) To use this in Spotfire, (npagratis might have been only working in S+) you would create a Data Function that expects "Column" inputs x and y, and returns a "Column" output z. The Data Function script would need only the lines defining window.size and z. When you embed the Data Function in your Spotfire analysis, map the output column to the same Data Table from which the inputs x and y are taken. After running the Data Function the first time, in your scatterplot you would select column z to color by.
Page 1 of 1 (4 items)
┬ęCopyright 2000-2011 TIBCO Software Inc | Privacy Policy | Terms of Use I Blog I Contact Us I Content Center