Search code examples
rplotstatisticsprobability

How to draw plots for multiple conditions in R?


I am new to R studio and I am trying to draw bar plots for a specific problem, which has multiple conditions. the problem is:

A producer guarantees that germinability of one special pea cultivar is 50%. A gardener bought 50 pea seeds. Calculate the probability that:

  1. all seeds will sprout,
  2. at most 5 seeds will sprout,
  3. at least 4 seeds will sprout.
  4. and so on...

Now, how can I draw bar plots for these? I am currently having this solution:

xP50 <- c(0:50)
prob1 <- dbinom(xP50,50,0.5)
tab1 <- data.frame(value = xP50, probability = prob1)
barplot(tab1$probability)

and for the second one, I have:

xP5 <- c(0:50)
prob2 <- dbinom(xP5,50,0.5)
tab2 <- data.frame(value = xP5, probability = prob2)
barplot(tab2$probability)

Is this the right way, to achieve the bar plots? Am I doing it correctly?

Thank You.


Solution

  • prob=dbinom(50, 50, .5)
    barplot(c(prob, 1-prob), names.arg=c("Sprout", "Not Sprout"), main="All 50 Seeds")
    

    enter image description here

    prob=pbinom(5, 50, .5)
    barplot(c(prob, 1-prob), names.arg=c("Sprout", "Not Sprout"), main="At Most 5 Seeds")
    

    enter image description here

    prob=pbinom(3, 50, .5, lower.tail=FALSE)
    barplot(c(prob, 1-prob), names.arg=c("Sprout", "Not Sprout"), main="At Least 4 Seeds")
    

    enter image description here

    xP50 <- c(0:50)
    prob1 <- dbinom(xP50,50,0.5)
    tab1 <- data.frame(x = xP50, probability = prob1)
    library(ggplot2)
    ggplot(tab1) + geom_histogram(aes(x, probability), stat="identity") + ggtitle("Probability of x pea seeds being germinable")
    

    enter image description here