我有以下R代码,我试图绘制一系列曲线。绘制曲线时绘制的值不正确。我已经检查了计算的值,它们与正在绘制的值不同。
代码(正在创建数据框):
maleHeightCentileDF <-data.frame(Age=numeric(),Values=numeric(),Centile=character())
femaleHeightCentileDF <- data.frame(Age=numeric(),Values=numeric(),Centile=character())
maleWeightCentileDF <- data.frame(Age=numeric(),Values=numeric(),Centile=character())
femaleWeightCentileDF <- data.frame(Age=numeric(),Values=numeric(),Centile=character())
for(i in 1:9)
{
maleHeightCentileDF<-
rbind(maleHeightCentileDF,data.frame(Age=maleLMSData$Months,Values=lmsFunctionToM(maleLMSData$L.ht,maleLMSData$M.ht,maleLMSData$S.ht,zValues[i]),Centile=pValues[i]))
femaleHeightCentileDF<-rbind(femaleHeightCentileDF,data.frame(Age=femaleLMSData$Months,Values= lmsFunctionToM(femaleLMSData$L.ht,femaleLMSData$M.ht,femaleLMSData$S.ht,zValues[i]),Centile=pValues[i]))
maleWeightCentileDF<-rbind(maleWeightCentileDF,data.frame(Age=maleLMSData$Months,Values= lmsFunctionToM(maleLMSData$L.wt,maleLMSData$M.wt,maleLMSData$S.wt,zValues[i]),Centile=pValues[i]))
femaleWeightCentileDF<-rbind(femaleWeightCentileDF,data.frame(Age=femaleLMSData$Months,Values= lmsFunctionToM(femaleLMSData$L.wt,femaleLMSData$M.wt,femaleLMSData$S.wt,zValues[i]),Centile=pValues[i]))
}
maleWeightCentileDF$Centile <- factor(maleWeightCentileDF$Centile, levels = rev(levels(maleWeightCentileDF$Centile)))
femaleWeightCentileDF$Centile <- factor(femaleWeightCentileDF$Centile, levels = rev(levels(femaleWeightCentileDF$Centile)))
maleHeightCentileDF$Centile <- factor(maleHeightCentileDF$Centile, levels = rev(levels(maleHeightCentileDF$Centile)))
femaleHeightCentileDF$Centile <- factor(femaleHeightCentileDF$Centile, levels = rev(levels(femaleHeightCentileDF$Centile)))
代码(绘制曲线的地方):
if(type=="wt")
{
if (gender=="Boys")
{
childDF <- data.frame(Age=maleChildrenData$Months[maleChildrenData$id==childID], Values=maleChildrenData$weight[maleChildrenData$id==childID])
plot<-ggplot(maleWeightCentileDF,aes(x=Age,y=Values, label = Centile))+geom_smooth(aes(colour=Centile),linetype='dotdash',se=FALSE)
plot<- plot+labs(x="Age (Months)",y="Weight (kg)") + scale_x_continuous(breaks=seq(0,60,5), limits = c(0,60))+scale_y_continuous(breaks = seq(0,30,1), limits = c(0,max(maleWeightCentileDF$Values)))
}
else
{
childDF <- data.frame(Age=femaleChildrenData$Months[femaleChildrenData$id==childID], Values=femaleChildrenData$weight[femaleChildrenData$id==childID])
plot<-ggplot(femaleWeightCentileDF,aes(x=Age,y=Values, label = Centile))+geom_smooth(aes(colour=Centile),linetype='dotdash',se=FALSE)
plot<- plot+labs(x="Age (Months)",y="Weight (kg)") + scale_x_continuous(breaks=seq(0,60,5), limits = c(0,60))+scale_y_continuous(breaks = seq(0,30,1), limits = c(0,max(femaleWeightCentileDF$Values)))
}
}
else
{
if (gender=="Boys")
{
childDF <- data.frame(Age=maleChildrenData$Months[maleChildrenData$id==childID], Values=maleChildrenData$height[maleChildrenData$id==childID])
plot<-ggplot(maleHeightCentileDF,aes(x=Age,y=Values, label = Centile))+geom_smooth(aes(colour=Centile),linetype='dotdash',se=FALSE)
plot<- plot+labs(x="Age (Months)",y="Height (cm)")+ scale_x_continuous(breaks=seq(0,60,5))+scale_y_continuous(breaks=seq(40,130,by=5), limits=c(40,max(maleHeightCentileDF$Values)))
}
else
{
childDF <- data.frame(Age=femaleChildrenData$Months[femaleChildrenData$id==childID], Values=femaleChildrenData$height[femaleChildrenData$id==childID])
plot<-ggplot(femaleHeightCentileDF,aes(x=Age,y=Values, label = Centile))+geom_smooth(aes(colour=Centile),linetype='dotdash',se=FALSE)
plot<- plot+labs(x="Age (Months)",y="Height (cm)")+ scale_x_continuous(breaks=seq(0,60,5))+scale_y_continuous(breaks=seq(40,130,by=5), limits=c(40,max(femaleHeightCentileDF$Values)))
}
}
第一个数据点应为(0,4.81),但绘制(0,6.02)(在图中突出显示)。
为什么会发生这种情况的原因?
答案 0 :(得分:0)
在对问题进行更多研究之后,我发现将曲线从geom_smooth绘制到geom_path的功能更改了问题。我不确定为什么这样可以解决问题,但问题已得到解决。