Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot of a Binomial Distribution for various probabilities of success in R

Tags:

plot

r

Is it possible to plot histogram-like bars/lines for a Binomial distributed random variable with different success probabilities next to each other in R?

The number of trials (n) and the sample space stays the same. Only the probability of success (p) is different. How has the R code to look like in order to have the bars next to each other?

Here is a small example what I mean:

yval <- 0:10 # sample space
plot(dbinom(yval, 10, 0.5) ~ yval, type = "h", col = "black", ylim = c(0, 0.35))
lines(dbinom(yval, 10, 1/6) ~ yval, type = "h", col = "red")
lines(dbinom(yval, 10, 0.6) ~ yval, type = "h", col = "green")
legend("topright", legend = c("p = 0.5", "p = 1/6", "p = 0.6"),
col = c("black", "red", "green"), lty = 1, cex = 0.7)

With this code the lines are plotted onto each other. What do I have to change here?

Thank you in advance.

like image 544
TopheR Avatar asked Nov 21 '25 13:11

TopheR


1 Answers

Do you mean as follows?

enter image description here

You can just add a small offset to the x-values of the other histograms:

yval <- 0:10 # sample space
plot(dbinom(yval, 10, 0.5) ~ yval, type = "h", col = "black", ylim = c(0, 0.35))
lines(dbinom(yval, 10, 1/6) ~ I(yval + 0.1), type = "h", col = "red") # + 0.1
lines(dbinom(yval, 10, 0.6) ~ I(yval + 0.2), type = "h", col = "green") # + 0.2
legend("topright", legend = c("p = 0.5", "p = 1/6", "p = 0.6"),
col = c("black", "red", "green"), lty = 1, cex = 0.7)
like image 185
Frans Rodenburg Avatar answered Nov 24 '25 07:11

Frans Rodenburg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!