3

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.

jpsmith
  • 11,023
  • 5
  • 15
  • 36
Michael
  • 1,537
  • 6
  • 20
  • 42

1 Answers1

4

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, e.g. if you were plotting line segments that should be part of a connected line (see example below).

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 ...

x <- 1:5; y <- c(1,4,2,3,5)
f2 <- function(le) {
   plot(x,y, type ="n", main = le)
   segments(x[-length(x)], y[-length(x)], x[-1], y[-1],
        lwd = 20, lend  = le)
}
par(mfrow=c(1,3))
invisible(lapply(c("round", "butt", "square"), f2))

enter image description here

Here you can see that the round end caps work well, both 'butt' and 'square' have issues. (I can't think offhand of a use case for "square", but I'm sure one exists ...) There is a good description of line-drawing parameters here (although it also doesn't suggest use cases ...)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Lovely. The documentation in ?par for the difference between "butt" and "square" is pretty minimal. Your explanation is helpful, although I'm still a bit confused when I'd want this extra extension. – Michael Nov 15 '22 at 22:59