Home > Back-end >  Plotting symbols in R as Pie Charts of the Weights
Plotting symbols in R as Pie Charts of the Weights

Time:10-29

Consider this toy example in R

x<-seq(0,1,length.out=10)
y<-sin(x)
plot(x,y)
set.seed(10)
weights <- matrix(runif(30),ncol=3)
weights <- sweep(weights,1,apply(weights,1,sum),"/")

where each point has probabilities associated with being in 3 groups. For example, the first observation has

     [,1]       [,2]      [,3]
 [1,] 0.2507483 0.32198731 0.4272644

0.25 probability of being in group 1, 0.32 of being in group 2 and 0.43 of being in group 3. How can I make a plotting symbol for each point to look like a mini pie chart that will colour the pie chart accordingly? For example, for the first point, the plotting symbol would look like this

pie(weights[1,],labels=NA)

enter image description here

CodePudding user response:

Using plotrix:

plot(-0.1:1, -0.1:1, "n")
for (i in 1:length(x)) {
  floating.pie(x[i], y[i], weights[i,], radius=0.05, col = c("#ffffff", "#d3d3d3", "#add8e6"))
}

Which produces:

enter image description here

  • Related