r中的理论密度分布图

时间:2013-02-26 17:43:49

标签: r graph plot ggplot2

我有以下数据:

type1 <- c(rep(1,49), rep(2, 30), rep(3, 20), rep(4,1))
type2 <- c(rep(1,10), rep(2, 20), rep(3, 40), rep(4,20), rep(5,10))
type3 <- c(rep(5,49), rep(4, 30), rep(3, 20), rep(2,1))

dat2 <- data.frame(dens = c(type1, type2, type3), lines = rep(c("a", "b", "c"), 
each = 100))

这是直方图:

require(ggplot2) 
ggplot(dat2, aes(x = dens, fill = lines)) + geom_bar(position="dodge")+ theme_bw()

enter image description here

密度图不完全正确,因为这是离散测量的:

ggplot(dat2, aes(x = dens, fill = lines)) + geom_density(alpha = 0.5)

我希望产生顺畅的理论分布。我尝试了以下的beta发行版,我不知道如何才能最好地适应上面的数据(dat2)。

dat3 <- data.frame(dens = c(rbeta(4000000, 1, 3, ncp = 0),
 rbeta(4000000, 2, 2, ncp = 0), rbeta(4000000, 3, 1, ncp = 0))
                   , lines = rep(c("a", "b", "c"), each = 4000000))
#Plot.
ggplot(dat3, aes(x = dens, fill = lines)) + geom_density(alpha = 0.5) + theme_bw() 

enter image description here

0 个答案:

没有答案