所以,我很着急,我把正常曲线拼凑在一起,但你可以用它来绘制两个“直方图风格”的图。
当然,如果我们拥有完整的数据集而不仅仅是摘要,那么获得曲线会更容易。我有点捏造了它们,但我认为这足以在这里得到大致的想法。
我不完全清楚你为什么要这样做,但你可以......
library(SDMTools) # Use this to get weighted means
testdata <- structure(list(Size=c(25L, 28L, 31L, 45L, 60L),
diseased=c(0L, 22L, 10L, 5L, 2L),
healthy=c(55L, 40L, 15L, 7L, 2L)),
.Names = c("Size", "diseased", "healthy"),
class = "data.frame",
row.names = c(NA, -5L))
barplot(testdata$healthy,
names.arg=paste(" ",testdata$Size),
col="light blue",
border="blue",
xlim=c(0,6),
ylim=c(0,70),
width=0.5,
space=1)
par(new=TRUE)
barplot(testdata$diseased,
col="pink",
border="red",
xlim=c(0,6),
ylim=c(0,70),
width=0.5,
space=c(2,1,1,1,1))
healthy_mean <- wt.mean(x=testdata$healthy,wt=testdata$Size)
healthy_sd <- wt.sd(x=testdata$healthy,wt=testdata$Size)
diseased_mean <- wt.mean(x=testdata$diseased,wt=testdata$Size)
diseased_sd <- wt.sd(x=testdata$diseased,wt=testdata$Size)
yfit_healthy <- as.data.frame(dnorm(0:max(testdata$healthy),
mean=healthy_mean,sd=healthy_sd))
names(yfit_healthy) <- "y"
yfit_diseased <- as.data.frame(dnorm(0:max(testdata$diseased),
mean=diseased_mean,sd=diseased_sd))
names(yfit_diseased) <- "y"
yfit_healthy$x <- seq(0,6,length.out=length(yfit_healthy$y))
yfit_diseased$x <- seq(0,6,length.out=length(yfit_diseased$y))
lines(yfit_healthy$x,
(max(testdata$healthy)*yfit_healthy$y)/max(yfit_healthy$y),
col="blue",lwd=2)
lines(yfit_diseased$x,
(max(testdata$diseased)*yfit_diseased$y)/max(yfit_diseased$y),
col="red",lwd=2)
这段代码让我: