3 回答
TA贡献1887条经验 获得超5个赞
stat_function旨在在每个面板中覆盖相同的功能。(没有明显的方法可以使函数的参数与不同的面板匹配)。
正如伊恩(Ian)所建议的那样,最好的方法是自己生成法线,并将其绘制为单独的数据集(这是您之前出错的地方-合并对于这个示例来说没有意义,如果仔细看,您会看到这就是为什么您会得到奇怪的锯齿图案)。
解决问题的方法如下:
dd <- data.frame(
predicted = rnorm(72, mean = 2, sd = 2),
state = rep(c("A", "B", "C"), each = 24)
)
grid <- with(dd, seq(min(predicted), max(predicted), length = 100))
normaldens <- ddply(dd, "state", function(df) {
data.frame(
predicted = grid,
density = dnorm(grid, mean(df$predicted), sd(df$predicted))
)
})
ggplot(dd, aes(predicted)) +
geom_density() +
geom_line(aes(y = density), data = normaldens, colour = "red") +
facet_wrap(~ state)
TA贡献1877条经验 获得超1个赞
我认为您需要提供更多信息。这似乎可行:
pg <- ggplot(dd, aes(Predicted_value)) ## need aesthetics in the ggplot
pg <- pg + geom_density()
## gotta provide the arguments of the dnorm
pg <- pg + stat_function(fun=dnorm, colour='red',
args=list(mean=mean(dd$Predicted_value), sd=sd(dd$Predicted_value)))
## wrap it!
pg <- pg + facet_wrap(~State_CD)
pg
我们为每个面板提供相同的均值和sd参数。读者可以练习获得面板特定的平均值和标准偏差*;)
'*'换句话说,不确定如何完成...
TA贡献1851条经验 获得超3个赞
如果您不想“手工”生成正态分布线图,仍要使用stat_function并排显示图形-那么您可以考虑使用在“ Cookbook for R”上发布的“ multiplot”函数替代facet_wrap。您可以从此处将多图代码复制到您的项目中。
复制代码后,请执行以下操作:
# Some fake data (copied from hadley's answer)
dd <- data.frame(
predicted = rnorm(72, mean = 2, sd = 2),
state = rep(c("A", "B", "C"), each = 24)
)
# Split the data by state, apply a function on each member that converts it into a
# plot object, and return the result as a vector.
plots <- lapply(split(dd,dd$state),FUN=function(state_slice){
# The code here is the plot code generation. You can do anything you would
# normally do for a single plot, such as calling stat_function, and you do this
# one slice at a time.
ggplot(state_slice, aes(predicted)) +
geom_density() +
stat_function(fun=dnorm,
args=list(mean=mean(state_slice$predicted),
sd=sd(state_slice$predicted)),
color="red")
})
# Finally, present the plots on 3 columns.
multiplot(plotlist = plots, cols=3)
- 3 回答
- 0 关注
- 2373 浏览
添加回答
举报