0

I am trying to plot a piechart in R using data present in a data-frame.It is possible that the values can be zero at times.In this situation ,R is throwing an error How to overcome this issue?

slices<-c(pu1_500,pu501_1000,pu1001_2000)
     cols <- c("red","blue","green")
     percentlabels<- round(100*slices/sum(slices), 1)
     pie3D(slices,main="piechart",col=cols,labels=percentlabels,explode=0.1)
     legend("topright", c("1-500","501-1000","1001-2000"), cex=0.8,fill=cols)

here let us suppose that ' pu501_1000 ' has value 0,then i get the following error:

Error in if (length(by) && by == 0 && length(del) && del == 0) return(from) : missing value where TRUE/FALSE needed

Sotos
  • 51,121
  • 6
  • 32
  • 66
pixey
  • 53
  • 8
  • 1
    Please provide a reproducible example (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). We don't know how `pu1_500, pu501_1000, pu1001_2000` look like. – Qaswed Jun 21 '16 at 10:07
  • I can't reproduce the error. If I take your code and set `slices<-c(0, 5, 7)` then I get a nice pie chart. – Martin Schmelzer Jun 21 '16 at 10:08
  • @MartinDabbelJuSmelter i am able to produce a normal pie chart . But if i try to use pie3D instead of pie ,then i am getting that error. I have included the necessary libraries. – pixey Jun 21 '16 at 10:41
  • @pixey as i said i only modified `slices` and it works for me with pie3D. – Martin Schmelzer Jun 21 '16 at 10:44
  • @pixey the only thing I could think of are outdated package versions... – Martin Schmelzer Jun 21 '16 at 11:03

1 Answers1

0

A bit outdated but I also run into the same error when plotting 3D pies when one of the elements had 0 value. In my case, it helped to just subset my "slices" excluding the element that had 0 value. I guess in the case above it would work like this:

pie3D(slices[c(1,3)], main= "piechart", col=cols, labels=percentlabels, explode=0.1)

Note: 1 & 3 are the positions of the non-zero elements in the "slices" vector.

Rishav
  • 3,818
  • 1
  • 31
  • 49
Yach
  • 357
  • 3
  • 16