[R] centroid of radial.plot differs from centroid.polygon
Stefan Uhmann
stefan.uhmann at mailbox.tu-dresden.de
Thu Aug 21 09:39:16 CEST 2008
Jim Lemon schrieb:
> On Wed, 2008-08-20 at 13:36 +0200, Stefan Uhmann wrote:
>> Dear R-Helpers,
>>
>> I need the centroid of circular data and (because the function used does
>> not provide the centroid coordinates, or did I miss sth.?) tried it via
>> the indirect way and just computed the cartesian coordinates:
>>
>> >>>
>> library(plotrix)
>> library(maps)
>>
>> #generate data
>> data1<-matrix(c(1.8,1.5,1.2,0.9,0.0,-0.9,-0.6,0.3)+1.2,nrow=1)
>> #radial plot
>> radial.plot(data1,labels=paste("Dim",1:8,sep=""),rp.type="s",
>> main="test",show.grid=TRUE, radial.lim=c(0,3.5), show.centroid=TRUE,
>> clockwise=T)
>> #compute cartesian coordinates
>> deg1<-seq(from=0,to=7,by=1)*pi/4
>> x=(data1)*cos(2*pi-deg1)
>> y=(data1)*sin(2*pi-deg1)
>> #print cartesian points (of polygon)
>> points(x,y,pch=18)
>> #print cartesian centroid
>> points(matrix(centroid.polygon(cbind(as.vector(x),as.vector(y))),
>> nrow=1), pch=17)
>> >>>
>>
>> My problem is, that the centroid computed by the radial.plot-function
>> (big unfilled circle) is somewhat different from the one computed 'by
>> hand' using cartesian coordiantes and centroid.polygon (small triangle).
>> I would really appreciate any suggestions how to obtain the correct
>> centroid and what is wrong here.
>>
> Hi Stefan,
> The radial.plot function just takes the mean of the x and y positions
> for the centroid (see line 119). If this is incorrect, I would greatly
> appreciate a pointer to the correct function for this so that I can
> modify the code.
>
> Jim
>
Dear Jim,
first of all, great work! The radial.plot function was the best and most
flexible one for me and my purposes.
I'm not into maths, but to me it appears that the centroid of a polygon
is sth. complicated - my only source is wikipedia, btw. - and not just
the mean of the x- and y-coordinates. A short glance brought up the
result, that various math packs (matlab, maple) have functions to
compute the centroid of a polygon. But I have to admit that I can not be
of any further help - the formulas found in wikipedia are greek for me.
However, it would be great if you could implement the centroid - and
maybe even let the function give it back - and just rename the current
function to sth. else.
/Stefan
More information about the R-help
mailing list