Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将饼图颜色替换为渐变色_R_Plot_Pie Chart - Fatal编程技术网

R 将饼图颜色替换为渐变色

R 将饼图颜色替换为渐变色,r,plot,pie-chart,R,Plot,Pie Chart,我有这个饼图 pie(c(1,2,1),col=c("black","white","gray")) 我想保持白色和黑色的原样,但我想用黑色到白色的渐变来改变灰色,黑色区域旁边的区域开始是黑色,然后逐渐变成灰色,然后在到达白色区域之前逐渐变成白色。因此,灰色将被以下内容取代: 你觉得我该怎么做?任何建议都将不胜感激。您可以使用ggplot2软件包 首先,重新排列数据: x <- c(1,2,1) labels <- c(1,2,3) df <- data.frame(x

我有这个饼图

pie(c(1,2,1),col=c("black","white","gray"))

我想保持白色和黑色的原样,但我想用黑色到白色的渐变来改变灰色,黑色区域旁边的区域开始是黑色,然后逐渐变成灰色,然后在到达白色区域之前逐渐变成白色。因此,灰色将被以下内容取代:


你觉得我该怎么做?任何建议都将不胜感激。

您可以使用
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))))

x您可以将一个部分细分为多个部分,并从一个比例向每个部分应用颜色。它需要为外圈绘制一条线,该线在饼图调用中删除

# 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)
#要细分的间隔数-增加以获得更精细的细节

n谢谢,就是这样
# 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)