user642080
user642080

Reputation: 99

Binomial histogram with ggplot2 function

Suppose I have a data set consisting of values of a statistic which theoretically follows Binomial distribution with some specified parameter (say size=30, prob=0.5). Suppose my dataset is represented by r which is given below:-

 dput(r)
c(16L, 7L, 12L, 19L, 15L, 11L, 13L, 16L, 14L, 15L, 11L, 12L, 
18L, 18L, 12L, 14L, 13L, 13L, 18L, 20L, 13L, 19L, 14L, 17L, 16L, 
19L, 14L, 12L, 14L, 12L, 16L, 13L, 16L, 15L, 18L, 17L, 12L, 16L, 
15L, 14L, 16L, 11L, 13L, 12L, 14L, 17L, 17L, 18L, 15L, 18L, 15L, 
13L, 12L, 13L, 16L, 13L, 13L, 12L, 18L, 11L, 16L, 16L, 13L, 18L, 
15L, 9L, 14L, 17L, 14L, 11L, 17L, 15L, 13L, 14L, 18L, 16L, 11L, 
13L, 13L, 11L, 21L, 11L, 17L, 7L, 15L, 15L, 16L, 16L, 13L, 10L, 
12L, 17L, 13L, 14L, 12L, 19L, 12L, 12L, 14L, 16L)

I want to see whether this observations are from binomial distribution with size=30, and prob=0.5. I want to do these using histogram. I tried the following codes :-

x<-data.frame(obs=dbinom(0:50, 50, 0.5))
y<-data.frame(obs=r)
x$type<-'bin1'
y$type<-'bin2'
data<-rbind(x,y)
library(devtools)
library(easyGgplot2)
ggplot2.histogram(data=data, xName='obs',
                  groupName='type', legendPosition="top",
                  alpha=0.2 )

This is the plot I get. But I think I have done mistake, because the histogram for x should have been symmetric, but it shows only one bar. Could anyone tell me where I have mistaken? enter image description here

I tried to use the following references while writing the codes:- 1)http://www.sthda.com/english/wiki/ggplot2-histogram-easy-histogram-graph-with-ggplot2-r-package

2)How to plot two histograms together in R?

Upvotes: 1

Views: 1465

Answers (1)

Wolfgang Arnold
Wolfgang Arnold

Reputation: 1252

I assume you rather wanted to use rbinom - so, would this be closer to the result you want to achieve?

x<-data.frame(obs=rbinom(30, 30, 0.5))
y<-data.frame(obs=r)
x$type<-'bin1'
y$type<-'bin2'
data<-rbind(x,y)

ggplot(data, aes(obs, fill = type)) + geom_histogram(position = "dodge", alpha = 0.8)

Upvotes: 1

Related Questions