Home > Software engineering >  why do I get Error in density.default while creating bean plot
why do I get Error in density.default while creating bean plot

Time:04-22

I am trying to create a beanplot:

library(beanplot)

beanplot(df, col="#cccccc", log="y", names="plot-1", yaxt="n", overallline="median", ll=0.00001)

However, I get the following error:

Error in density.default(mlog(x), bw = bw, kernel = kernel, from = from,  : 
  non-finite 'from'

Can anyone please help?

CodePudding user response:

It's impossible to be sure without seeing your data, but I suspect that your problem comes from having one or more zeros in the input then trying to draw its density on a log scale.

I can replicate your problem like this:

library(beanplot)

df <- data.frame(x = 0:10)

beanplot(df, col="#cccccc", log = 'y', names="plot-1", 
         overallline="median", ll=0.00001)
#> Error in density.default(mlog(x), bw = bw, kernel = kernel, from = from,  : 
#>   non-finite 'from'

If we remove the zeros, we get a plot:

beanplot(df[df$x != 0,], col="#cccccc", log = 'y', names="plot-1", 
         overallline="median", ll=0.00001)

enter image description here

Or we can remove the log scale and keep the zeros:

beanplot(df, col="#cccccc", names="plot-1", overallline="median", ll=0.00001)

enter image description here

But clearly we can't both keep the zeros and have a log scale, since the log of zero is interpreted as negative infinity in R.

  • Related