用渐变色替换饼图颜色

4
我有这个饼图。
pie(c(1,2,1),col=c("black","white","gray"))

enter image description here

我希望保留白色和黑色,但是想用从黑到白的渐变色来替换灰色。在靠近黑色区域的部分,颜色从黑色开始逐渐变成灰色,然后再逐渐变成白色,直到达到白色区域。因此,灰色将被替换为类似于这样的颜色:

enter image description here

任何想法我怎样才能做到这一点?非常感谢您的任何建议。
2个回答

5
你可以将一个区域细分为多个小区域,并为每个小区域应用来自比例尺的颜色。这需要在外圆上画一条线,然后在饼图中删除它。
# Number of intervals to subdivide - increase for finer detail
n <- 41 
# Generate colours
cols <- colorRampPalette(c("white", "black"))(n) 

# Plot
# lty=0 removes the section lines, which also removes outer border
pie(c(1,2, rep(1/n, n)), col=c("black","white", cols) , lty=0,
                                    labels=c(1,2, rep("", n/2), 3))

# Add in outer circle back in
# radius=0.8 used as this is the pie default
plotrix::draw.circle( 0,0, 0.8)

这提供了什么?

在此输入图像描述


1
谢谢,这就是我想要的。 - Oposum

1
你可以使用ggplot2包。
首先,重新排列你的数据:
x <- c(1,2,1)
labels <- c(1,2,3)
df <- data.frame(x = unlist(mapply(x = x, lab = labels, function(x, lab) rep(lab, times = x))))

接下来,这里是绘图的代码。
pie <- ggplot(df, aes(x = factor(1), fill = factor(x)))
pie <- pie + geom_bar(width = 1)
pie <- pie + coord_polar(theta = "y") 
pie <- pie + xlab("") + ylab("")
pie + scale_fill_grey()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接