Search code examples
rggplot2histogrampoisson

ggplot histogram of Poisson distribution


How to make a ggplot histogram of a Poisson distribution with lambda = 2.5? x-axis = 0:10.

In this histogram, I need to indicate P(X>=4) with colors, being x=0:4 one color and x=5:10 another color.

Thank you so much.


Solution

  • Here is the code for that. R includes a function for generating data according to the most common distributions. You only need to classify according to your criteria.

    # Generate data
    d <- rpois(n = 10e5, lambda = 2.5)
    
    # categorise the data according to your criteria
    data <- data.frame(d = d, 
                       col = ifelse(d < 5, "red", "blue"))
    
    library(ggplot2)
    
    ggplot(data, aes(x = d, fill = col)) +
      geom_histogram(bins = 14, color = "black")
    

    enter image description here

    However, it is recommended to do more research before posting a question.