在R中的GGPLOT2中一起使用stat_function和facet_wrap [英] using stat_function and facet_wrap together in GGPLOT2 in R

查看:209
本文介绍了在R中的GGPLOT2中一起使用stat_function和facet_wrap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用GGPLOT2绘制格型数据,然后在样本数据上叠加一个正态分布来说明基础数据的正常范围。我希望有一个正常的发展方向与面板具有相同的意思和标准。



下面是一个例子:

  library(ggplot2)$ b $ (矩阵(144,平均= 2,sd = 2),72,2),c(代表(A,24 ),rep(B,24),rep(C,24)))
colnames(dd)< -c(x_value,Predicted_value,State_CD)

#This works
pg< - ggplot(dd)+ geom_density(aes(x = Predicted_value))+ facet_wrap(〜State_CD)
print(pg)

这一切都很好,并产生了一个很好的数据面板图。我如何在顶部添加正常的dist?看来我会用stat_function,但是这样做会失败:

 #这个失败
pg< - ggplot(dd) + geom_density(aes(x = Predicted_value))+ stat_function(fun = dnorm)+ facet_wrap(〜State_CD)
print(pg)

看来stat_function与facet_wrap功能不兼容。我怎么让这两个玩得很好?



------------编辑---------



我试图整合下面两个答案的想法,但我仍然不在那里:

  library(ggplot)
library(plyr)

#生成一些示例数据
dd <-data.frame(matrix(rnorm(108,mean = 2,sd = 2),36,2),c(rep(A (dd)<-c(x_value,Predicted_value,State_CD)$($) (dd,c(State_CD),函数(df)mean(df $ Predicted_value))
colnames(DevMeanSt)< -c(State_CD),b
$ b DevMeanSt < (,mean))
DevSdSt < - ddply(dd,c(State_CD),function(df)sd(df $ Predicted_value))
colnames(DevSdSt)< - c(State_CD ,sd)
DevStatsSt < - merge(DevMeanSt,DevSdSt)

pg < - ggplot(dd,aes(x = Predicted_value))
pg < - pg + geom_density()
pg < - pg + stat_function(fun = dnor m,color ='red',args = list(mean = DevStatsSt $ mean,sd = DevStatsSt $ sd))
pg < - pg + facet_wrap(〜State_CD)
print(pg)

这是非常接近的......除非正常的dist绘图出现错误:





我在这里做错了什么?

解决方案

stat_function 旨在覆盖每个面板中的相同功能。 (没有明显的方法可以将函数的参数与不同的面板进行匹配)。



正如伊恩所说,最好的方法是自己生成正常曲线,将它们绘制为一个单独的数据集(这是您之前出错的地方) - 合并对这个示例没有意义,如果仔细观察,您会看到这就是为什么您会遇到奇怪的问题锯齿图案)。



以下是我如何解决问题的方法:

  dd <-data.frame(
predict = rnorm(72,mean = 2,sd = 2),
state = rep(c(A,B,C),每个= 24)


grid < ddply(dd,state,function(df){
data.frame(
predict = grid,
density = dnorm(grid,mean(df $ predicted),sd(df $预计))

})

ggplot(dd,aes(预测))+
geom_density()+
geom_line(aes(y =密度),data = normaldens,color =red)+
facet_wrap(〜state)


I am trying to plot lattice type data with GGPLOT2 and then superimpose a normal distribution over the sample data to illustrate how far off normal the underlying data is. I would like to have the normal dist on top to have the same mean and stdev as the panel.

here's an example:

library(ggplot2)

#make some example data
dd<-data.frame(matrix(rnorm(144, mean=2, sd=2),72,2),c(rep("A",24),rep("B",24),rep("C",24)))
colnames(dd) <- c("x_value", "Predicted_value",  "State_CD")

#This works
pg <- ggplot(dd) + geom_density(aes(x=Predicted_value)) +  facet_wrap(~State_CD)
print(pg)

That all works great and produces a nice three panel graph of the data. How do I add the normal dist on top? It seems I would use stat_function, but this fails:

#this fails
pg <- ggplot(dd) + geom_density(aes(x=Predicted_value)) + stat_function(fun=dnorm) +  facet_wrap(~State_CD)
print(pg)

It appears that the stat_function is not getting along with the facet_wrap feature. How do I get these two to play nicely?

------------EDIT---------

I tried to integrate ideas from two of the answers below and I am still not there:

using a combination of both answers I can hack together this:

library(ggplot)
library(plyr)

#make some example data
dd<-data.frame(matrix(rnorm(108, mean=2, sd=2),36,2),c(rep("A",24),rep("B",24),rep("C",24)))
colnames(dd) <- c("x_value", "Predicted_value",  "State_CD")

DevMeanSt <- ddply(dd, c("State_CD"), function(df)mean(df$Predicted_value)) 
colnames(DevMeanSt) <- c("State_CD", "mean")
DevSdSt <- ddply(dd, c("State_CD"), function(df)sd(df$Predicted_value) )
colnames(DevSdSt) <- c("State_CD", "sd")
DevStatsSt <- merge(DevMeanSt, DevSdSt)

pg <- ggplot(dd, aes(x=Predicted_value))
pg <- pg + geom_density()
pg <- pg + stat_function(fun=dnorm, colour='red', args=list(mean=DevStatsSt$mean, sd=DevStatsSt$sd))
pg <- pg + facet_wrap(~State_CD)
print(pg)

which is really close... except something is wrong with the normal dist plotting:

what am I doing wrong here?

解决方案

stat_function is designed to overlay the same function in every panel. (There's no obvious way to match up the parameters of the function with the different panels).

As Ian suggests, the best way is to generate the normal curves yourself, and plot them as a separate dataset (this is where you were going wrong before - merging just doesn't make sense for this example and if you look carefully you'll see that's why you're getting the strange sawtooth pattern).

Here's how I'd go about solving the problem:

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) 

这篇关于在R中的GGPLOT2中一起使用stat_function和facet_wrap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆