R 当i';我正在使用ggplot2

R 当i';我正在使用ggplot2,r,ggplot2,histogram,density-plot,R,Ggplot2,Histogram,Density Plot,因此,正如您在上传的图片中所看到的,我的密度条范围因我使用的型号(基本R或ggplot2)而异 e、 g代表115上的蓝色条,ggplot2中的条垂直,但当我使用底部R时,不会垂直 我已经尝试过这个链接(inside-r.org/r-doc/stats/density),但我对r还是新手,没有找到解决方案 基本R Immature2=subset(dataHistogram2, Sex=='I') Female2=subset(dataHistogram2, Sex=='F') Male2=su

因此,正如您在上传的图片中所看到的,我的密度条范围因我使用的型号(基本R或ggplot2)而异 e、 g代表115上的蓝色条,ggplot2中的条垂直,但当我使用底部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)

我认为范围没有改变,可能只是不同的装箱策略。尝试将
right=TRUE
添加到
geom\u histogram()
是的,正如joran所说,箱子是不同的
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)))