Home > Software engineering >  R: plot() Function with type="h" Misrepresents Small Numbers ( For Larger Values of "
R: plot() Function with type="h" Misrepresents Small Numbers ( For Larger Values of "

Time:11-16

I am trying to generate a plot showing the probabilities of a Binomial(10, 0.3) distribution.

I'd like to do this in base R.

The following code is the best I have come up with,

plot(dbinom(1:10, 10, 0.3), type="h", lend=2, lwd=20, yaxs="i")

My issue with the above code is the small numbers get disproportionately large bars. (See below) For example P(X = 8) = 0.00145 but the height in the plot looks like about 0.025.

Binomial(10, 0.3) Distribution

It seems to be an artifact created by wanting wider bars, if the lwd = 20 argument is removed you get tiny bars but their heights seem to be representative.

CodePudding user response:

I think the problem is your choice of lend (line-end) parameter. The 'round' (0) and 'square' (2) choices are intended for when you want a little bit of extra extension beyond the end of a segment, e.g. so that adjacent segments join nicely.

f <- function(le) plot(dbinom(1:10, 10, 0.3), 
        type="h", lend = le, lwd=20, yaxs="i", main = le)
par(mfrow=c(1,3))
invisible(lapply(c("round", "butt", "square"), f))

enter image description here

"round", "butt", and "square" could also be specified (less mnemonically) as 0, 1, and 2 ...

  • Related