Replace the color of the pie chart with a gradient

I have this pie chart

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

enter image description here

I would like to keep the white and black colors as they are, but I want to change the gray color with a black and white gradient, where the area next to the black sector starts with black, then gradually turns gray, and then gradually turns white before it reaches white sector. Thus, the gray color will be replaced with something like this:

enter image description here

Any thoughts how I can do this? Any advice would be appreciated.

+4
source share
2 answers

You can divide a section into several sections and apply color from the scale to each. To do this, draw a line for the outer circle, which is removed in the pie call.

# 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)

enter image description here

+5

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()
+1

Source: https://habr.com/ru/post/1619738/


All Articles