【问题标题】:Pie Slices: Circle Segments for Pie Charts With Different Radii in R饼片:R 中具有不同半径的饼图的圆段
【发布时间】:2020-10-23 21:26:01
【问题描述】:

我想创建一种不同类型的饼图,在其中我不仅可以指定每个段的百分比 (x \in [0,1]),还可以指定每个饼段的半径延伸多远。 (想法是显示第二个维度,它是 x 和其他变量的乘积。)我还需要填充和标记这些段。如果函数自己标记或返回每个段外弧的中点,这将是最简单的。

(如果没有人写过这个,我需要的原始函数是一个实心圆段,中心 x,y 从角度 a1 延伸到半径 r 到 a2。来自 plotrix 的draw.arc() 看起来不错,但 col 没有填充[它只设置外部颜色]。)

这样的功能是否已经存在,还是我需要自己编写?指针表示赞赏。

【问题讨论】:

  • 谢谢,我做到了(抱歉——我应该提到它),但它没有回答。它似乎是一种非常特殊的径向图,不容易推广。

标签: r geometry pie-chart


【解决方案1】:
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()

得到一个像

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    相关资源
    最近更新 更多