【发布时间】:2016-03-30 18:05:47
【问题描述】:
我正在为分配给我的任务而苦苦挣扎。 我们必须制作一个叠加了正常拟合的分组直方图。 现在,我已经设法在 Basic R graph、Lattice 和 Ggplot 中获得了分组直方图。在 Basic R graph 中,我也能得到一条正态曲线,但在 Lattice 和 Ggplot 中,我似乎没有这样做。
这是我来自 Lattice 和 Ggplot 的 R 脚本:
#Lattice:
library(lattice)
histogram(~SBP, data= DataSBP, breaks=10,
type=c("density"),
groups = User, panel = function(...)panel.superpose(...,panel.groups=panel.histogram, col=c("navy","maroon3"),alpha=0.4),
auto.key=list(columns=2,rectangles=FALSE, col=c("navy","maroon3")))
panel.mathdensity(dmath=dnorm, col="black", args=list(mean=mean(x, na.rm = TRUE), sd=sd(x, na.rm = TRUE)))
当我尝试命令“panel.mathdensity”时没有任何反应。
# Ggplot
library(ggplot2)
ggplot(DataSBP, aes(x=SBP)) + geom_histogram(aes(y=..density.., x=SBP, colour=User, fill=User),alpha=0.5, binwidth = 5, position="identity")
+ stat_function(fun = dnorm, args = list(mean = SBP.mean, sd = SBP.sd))
如果我尝试 stat_function 命令,我总是得到错误“SBP.mean”找不到,这可能意味着我必须定义 SBP.mean,但是如何?
我的数据是这样的:
User SBP
No 102
No 116
No 106
...
Yes 117
Yes 127
Yes 111
...
我的图表如下所示:
【问题讨论】: