# Probability distribution of a random variable

The table below shows the number of ice creams brought and the number of customers brought that number of ice creams.

 ice_cream     No_of_customers
1             225
2             170
3              55
4              20
5              20
6              10


The estimated probability of purchasing the number of ice cream is shown below. e.g. probability of purchasing 1 ice cream is 225/500 = 0.45. This table below shows that distribution of number of ice cream sold

ice_cream    Probability
1        0.45
2        0.34
3        0.11
4        0.04
5        0.04
6        0.02


while showing this above distribution as a graph with the below code I was expecting my y- axis in histogram plot to have same values as in probability column

 ggplot(data = ice_cream)+
geom_histogram(aes(x = ice_cream,y = ..density..))


I tried density plot but y axis is not the one in probability column for each ice cream column. I am missing something very basic here.

Can someone please suggest how to get that probability in the graph?

Sample data:

ice_cream <- data.frame(ice_cream = c(rep(1,225),rep(2,170),rep(3,55),rep(4,20),rep(5,20),rep(6,10)))


The issue is that you're not specifying the binwidth when constructing your histrogram even though the discrete nature of your data makes that critical. Try:

ggplot(data = ice_cream) +
geom_histogram(aes(x = ice_cream, y=..density..),
binwidth=1,
colour="black", fill="white")


Note that we specified binwidth = 1.

To see how things can go wrong, try setting binwidth = 0.5 and observe what happens. As I wrote below in the comment, the problem with omitting binwidth entirely is that it will default to 30 bins and treat your data as continuous, so that you are now estimating a density of a continuous variable, which can certainly be greater than $$1$$ at certain regions. Here, at each of these $$30$$ bins that does not contain an integer, the density is $$0$$, and at bins that do contain an integer $$k$$, the density is $$P(\text{icecream} = k)/\text{binwidth}$$ and since $$\text{binwidth} < 1$$, this is inflating the numbers.

• Thanks. That solve the problem. What does the value in y axis means if the bandwidth is not specified Apr 15 at 15:43
• When you don't specify binwidth or bins and run the code I provide, you get a warning message saying "stat_bin() using bins = 30. Pick better value with binwidth." So it's resorting to 30 bins, and treating the observed data as continuous. So then you're just seeing a histogram for continuous data, with y axis showing the density of a continuous distribution, which can certainly be greater than 1. Here, at each bin that contains an integer $k$, its doing something like $P(\text{icecream} = k)/\text{binwidth}$, and since binwidth < 1, it's inflating the numbers! Does that make sense? Apr 15 at 15:56
• Thanks a ton for answering in intuitive way Apr 15 at 16:32

A couple ways to do this:

a) Plot the probability column using the probability column and geom_col

library(tidyverse)
ice_cream = 1:6
no_cust = c(225, 170, 55, 20, 20, 10)
d = tibble(ice_cream, no_cust)

d %>%
mutate(y = no_cust/sum(no_cust)) %>%
ggplot(aes(ice_cream, y))+
geom_col()



b) uncount the ice creams

library(tidyverse)
ice_cream = 1:6
no_cust = c(225, 170, 55, 20, 20, 10)
d = tibble(ice_cream, no_cust)

d %>%
uncount(weights = no_cust) %>%
ggplot(aes(ice_cream))+
geom_histogram(aes(y = ..count../sum(..count..)))

$$$$
`