While searching the web, solutions for finding centroids of polygons come up rather often. What I'm interested in is finding a centroid of a cluster of points. A weighted mean of sorts.

Can you provide some pointers, pseudo code (or even better, an R package that has already solved this) or links of how this issue can be tackled?

@iant has suggested a method to average coordinates and use that for the centroid. This is exactly what crossed my mind when I saw the right picture on this web page.

Here is some simple R code to draw the following figure that demonstrates this (× is the centroid):

xcor <- rchisq(10, 3, 2)
ycor <- runif(10, min = 1, max = 100)
mx <- mean(xcor)
my <- mean(ycor)

plot(xcor, ycor, pch = 1)
points(mx, my, pch = 3)

enter image description here

cluster::pam()$medoids returns a medoid of a set of cluster. This is an example from @Joris Meys:

df <- data.frame(X = rnorm(100, 0), Y = rpois(100, 2))
plot(df$X, df$Y)
points(pam(df, 1)$medoids, pch = 16, col = "red")
  • 1
    Is there a reason the mean center or center of minimum distance of the points won't suffice? – Andy W Feb 10 '11 at 15:58
  • 1
    @Roman: The graphic is incorrect: you need to use the mean, not the median. For 2D spatial point clouds there are analogs of a median center, but this is not one of them (because it is coordinate-dependent): see stats.stackexchange.com/q/1927/919 for a discussion. – whuber Feb 10 '11 at 17:34
  • 1
    I would also suggest checking out chapter 4 of the crimestat workbook, icpsr.umich.edu/CrimeStat/files/CrimeStatChapter.4.pdf. It is a pretty gentle intro, describes and graphically displays why the median for higher dimensions does not have a unique solution, and describes other measures of central tendency and variance of spatial point patterns. – Andy W Feb 10 '11 at 17:53
  • This is getting more and more interesting. Thank you for your answers. I'm looking into the matter. – Roman Luštrik Feb 10 '11 at 18:50
  • 2
    "suggested a method to average coordinates and use that for the centroid." This is, in fact, the definition of the centroid, not simply something which makes a good approximation. – Colin K Feb 10 '11 at 21:06

just average the X and Y coordinates (multiply by a weight if you want) and there is your centroid.

| improve this answer | |
  • 5
    +1 Great solution. It extends to centroids on the spheroid, too (which is essential for avoiding projection-related distortions when the points are spread over a large portion of the globe): first convert (lat, lon) to 3D (x,y,z) (geocentric) coordinates, average them, then convert the result back to (lat, lon) (ignoring the almost inevitable fact that the 3D average will be deep below the surface). – whuber Feb 10 '11 at 16:33
  • I've updated my question to reflect your answer. – Roman Luštrik Feb 10 '11 at 16:51
  • why are use suggesting to use a weight? – Herman Toothrot Mar 11 at 10:47
  • sometimes people want a weighted centroid (e.g. Population weighted centroid to pull centroid towards city centre) – Ian Turton Mar 11 at 10:52

You can use the centroid function from geosphere package.


| improve this answer | |
  • 1
    Welcome to GIS StackExchange and thanks for submitting an answer. Please take a moment to review the Tour to learn about our focused Q&A format. Please edit your answer to include more details as we are generally looking for longer (not 1-2 sentence) answers to help the original poster or future searchers. One modification you can do is to include a reason you think this tool will be helpful or a code snippet / screenshots. – smiller Nov 16 '18 at 17:00
  • @smiller this only works if you have at least 4 points. – Herman Toothrot Mar 11 at 10:45
  • 1
    @ Herman Toothrot Was this comment meant for @Leonardo Leite Ferraz de Campo, who composed the answer? – smiller Mar 11 at 13:43

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.