pie.slice <- function( center, radius, arc1, arc2, ..., fine=1, label=NULL, label.dist=1 ) {
stopifnot( length(arc1) == 1 )
stopifnot( length(arc2) == 1 )
stopifnot( (arc1 >= 0.0) & (arc1 <= 1.0) )
stopifnot( (arc2 >= 0.0) & (arc2 <= 1.0) )
stopifnot( arc2 > arc1 )
stopifnot( length(center) == 2)
stopifnot( length(radius) <= 2)
if (length(radius)==1) radius <- c(radius,radius)
xb <- seq(arc1, arc2, length.out= (arc2 * 180 - arc1 * 180) * fine)
x <- center[1] + c( 0, cospi(2*xb), 0 )*radius[1]
y <- center[2] + c( 0, sinpi(2*xb), 0 )*radius[2]
polygon( x, y, ... )
mid <- (arc1 + arc2)/2
anchor <- c( center[1] + cospi(2*mid )*radius[1], center[2] + sinpi(2*mid)*radius[2] )
if (!is.null(label))
text( anchor[1], anchor[2], label, adj=c(-0.5,1.0 )*label.dist, srt= mid*360 )
anchor ## returns the anchor point halfway for your own text drawing
}
为了演示,
pdf("pieslice.pdf", width=12, height=12, pointsize=12/0.75)
plot( 0, xlim=c(-1,2), ylim=c(-1,2), xlab="", ylab="", type="n" )
radius <- 0.04
sl.out <-seq(0.0, 1.0, 0.1)
my.center <- c(-0.25,1)
for (i in 2:(length(sl.out)-1))
pie.slice( my.center, radius*i, sl.out[i-1], sl.out[i], label=LETTERS[i], density=i*2 )
my.center <- c(1.0,0.0)
for (i in 2:length(sl.out))
pie.slice( my.center, 1.5*radius*(length(sl.out)-i+1), sl.out[i-1], sl.out[i],
label=paste("from ", sl.out[i-1], " to ", sl.out[i]), col=i, label.dist=0.25 )
dev.off()
得到一个像