因此,您可以在上传的图片中看到我的密度条范围因我使用的模型而异(Base R或ggplot2) 例如,对于115中的蓝色条,ggplot2中的条形垂直下降但是当我使用Base R时它没有。
我已经尝试过这个链接(inside-r.org/r-doc/stats/density),但我是R的新手,并没有找到解决方案。
基础R
Immature2=subset(dataHistogram2, Sex=='I')
Female2=subset(dataHistogram2, Sex=='F')
Male2=subset(dataHistogram2, Sex=='M')
hist(Immature2$Diameter, prob=TRUE ,breaks= seq(55,125, by=5), ylim=c(0,0.05), xlim=c(55,125), col=rgb(0,1,0,1/2), main="", xlab= "Diameter", ylab="Densiteit")
hist(Female2$Diameter, prob=TRUE, add=TRUE, breaks= seq(55,125, by=5), col=rgb(1,0,0,1/2))
hist(Male2$Diameter, prob= T,breaks=seq(55,125, by=5), add=T, col=rgb(0,0,1,1/2))
x=seq(55,125,0.01)
curveImmature2<-curve(dnorm(x,mean=mean(Immature2$Diam), sd=sd(Immature2$Diam)), add= TRUE, col=rgb(0,1,0,1/2), lwd=2)
curveFemale2 <- curve(dnorm(x,mean=mean(Female2$Diam), sd=sd(Female2$Diam)), add= TRUE, col= rgb(1,0,0,1/2), lwd=2)
curveMale2 <- curve(dnorm(x, mean=mean(Male2$Diam), sd=sd(Male2$Diam)), add= TRUE, col=rgb(0,0,1,1/2), lwd=2)
GGPLOT2
ggplot(dataHistogram2, aes(x=Diameter)) +
geom_histogram(binwidth=5, aes(y=..density.., colour=Sex, fill= Sex), position="identity", alpha=0.5)+xlim(55,125) +
stat_function(fun=dnorm, colour = "green", args=list(mean=mean(Immature2$Diameter), sd=sd(Immature2$Diameter))) +
stat_function(fun=dnorm, colour = "red", args=list(mean=mean(Female2$Diameter), sd=sd(Female2$Diameter))) +
stat_function(fun=dnorm, colour = "blue", args=list(mean=mean(Male2$Diameter), sd=sd(Male2$Diameter)))