1

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.

PatrickT
  • 10,037
  • 9
  • 76
  • 111
Carsten
  • 37
  • 7
  • 1
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Sotos Nov 10 '17 at 08:27
  • 1
    Possible duplicate of [Poisson density curve, histogram and shaded area with ggplot2?](https://stackoverflow.com/questions/47122281/poisson-density-curve-histogram-and-shaded-area-with-ggplot2) by the same user. – missuse Nov 10 '17 at 08:28

1 Answers1

2

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.

PatrickT
  • 10,037
  • 9
  • 76
  • 111
Santi
  • 368
  • 2
  • 15
  • thanks a lot, Santi - and yes, I am seeing that I need to study some more before posting these questions - thanks and sorry! – Carsten Nov 10 '17 at 08:47
  • You are welcome. If the answer is correct, then please, mark it as accepted (upper arrow), so that future users can find it easily. – Santi Nov 10 '17 at 08:53