I have a data frame of 3 columns. The first one is the response variable the second and the third ones are some criteria. You can create your own example similar to mine, using this piece of code with one difference; I have 120k of these rows.


      response   x1  x2
1  0.007240072 0.99  94
2  0.585625664 3.26 175
3  0.060195378 1.52 153
4  0.806096047 1.90  15
5  0.715590971 2.87 161
6  0.840640566 3.06  73
7  0.757785139 3.38 125
8  0.835112330 1.43 158
9  0.588479082 1.68  59
10 0.963268147 0.54 108

I would like to cluster the response column using x1 and x2 as the sides of a grid (non overlaping rectangular clusters while covering the whole population). So any cluster will have 4 boundaries (min x1 - max x1 - min x2 - max x2 for each cluster) or just the vector of split points (x1.1, x1.2, x1.3 and x2.1, x2.2, x2.3 for nine rectangular cluster). Just like a basic Mondrian painting


And I would like to plot a heat map using the averages of the response column in each cluster or the number of instances in each cluster.

For the first part I used ctree function from party package. My attempts to use rpart function from the rpart package is thwarted by the 'intimidating' size of the data. See my question about this problem I managed to get the terminal nodes but not the node conditions.

And how do I heat map such an object or data if I am given the split values?

  • 1
    $\begingroup$ This question on the GIS site has an example of building a quadtree to create irregular bins of a minimum size n. That being said, if this is only for a visualization I'm skeptical it is the most effective plot. Making a raster plot of the averages used e.g. inverse distance weighting (or simply a raster plot of the continuous kernel density estimate) is likely an easier visualization to evaluate. $\endgroup$
    – Andy W
    May 28, 2014 at 1:45
  • $\begingroup$ I provided an answer to the final question, but @whuber's comment make me wonder about your intent. Can you clarify? Are you looking for help with the partitioning or the graph making, or both? If only the graph making, then perhaps this is more or a programming question for Stack Overflow. $\endgroup$
    – xan
    May 28, 2014 at 18:28
  • $\begingroup$ The important part is clustering, heat map is only for visualization but it would be very good to have. I suppose when I have the necessary information (limits and cluster averages) I can do the rest. But if anyone had similar experience I also would like to know the answer. Therefore, your answer is also appreciated :) $\endgroup$
    – berkorbay
    May 30, 2014 at 12:11
  • $\begingroup$ @AndyW the quadtree suggestion is really nice, I'll look into it. $\endgroup$
    – berkorbay
    May 30, 2014 at 12:13

2 Answers 2


If you have the split values for the nx and ny spans, you can create a table of nx * ny rectangles and summarized values (sum, mean, whatever) and use geom_rect. That is, your derived table should have variables for each of the four rectangle coordinates and a value for coloring.

  • $\begingroup$ I believe the concern of the question is how to compute the clusters in the first place. Once the clusters have been found, the mechanics of visualizing them are pedestrian--the choice of visualization has already been stipulated--and are best discussed on Stack Overflow. $\endgroup$
    – whuber
    May 28, 2014 at 16:02
  • $\begingroup$ @whuber, that's what I thought at first, but the last part of the question says "And how do I heat map such an object or data if I am given the split values?" $\endgroup$
    – xan
    May 28, 2014 at 17:46
  • 1
    $\begingroup$ That's the part of the question that we would ordinarily send over to Stack Overflow: it's not on topic here. However, many people often volunteer working code in order to make their answers complete and fully helpful, so if you can first describe a method to find the clusters, then the rest of your answer is fine. $\endgroup$
    – whuber
    May 28, 2014 at 17:53

I think I found what I needed (close enough), heat mapping will perhaps be possible afterwards. partition.tree function from tree R package does something similar to grid-like clustering.

See an example below and the source here.

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.