[R] pie chart in lattice - trellis class
Patrick Giraudoux
patrick.giraudoux at univ-fcomte.fr
Mon May 28 15:05:07 CEST 2007
Jim Lemon a écrit :
> Patrick Giraudoux wrote:
>> Dear all,
>>
>> After going through the Lattice doc and R-help list and google, I got
>> the feeling that there is no function in lattice or other package to
>> compute a pie chart object of class "trellis". Although pie charts
>> are obviously not considered optimal even in the pie() doc ;-) , pie
>> chart trellis objects would be easy positioned e.g. over a map drawn
>> with the grids package.
>>
>> Can anybody confirm this absence or indicate a package/function able
>> to draw a pie chart object of class trellis?
>>
> Hi Patrick,
> Floating.pie was written to solve this problem, but it only works in
> standard graphics. Perhaps it might help, though.
>
> Jim
Hey ! Looks like exactly the one I was looking for... For info to other
R-users, it can be found in the 'plotrix' package. Meanwhile I had
dropped some line codes with the same target, still not fully achieved
(the argument center is not taken into account yet... needs one line more).
Thanks a lot!
Patrick
polycirc2<-function (radius=1, center = c(0, 0), edges = 50, init=pi/2,
angle=pi/2)
{
circ = NULL
angles <- seq(init, init+angle, l = edges)
for (i in angles) {
circ <- rbind(circ, cbind(radius * sin(i), radius * cos(i)))
}
circ<-rbind(c(0,0),circ)
return(cbind(circ[, 1] + center[1], circ[,2] + center[2]))
}
pie2<-function(x,col=NULL,...){
x<-x/sum(x)
xC<-cumsum(x)
xC<-xC/xC[length(xC)]
init=0
for (i in 1:length(x)) {
angle<-x[i]*2*pi
polygon(polycirc2(init=init,angle=angle),col=col[i],...)
init<-xC[i]*2*pi
}
}
# example
ex<-rpois(10,2)
plot(c(-1,+1),c(-1,+1),type="n",asp=1)
pie2(ex,col=c(1:length(ex)))
More information about the R-help
mailing list