问题描述
我有一个由不同数据集派生的线性和非线性模型列表,它们测量相同的两个变量 x
和 y
我想用 stat_smooth
绘制在同一个图上。这是为了能够轻松地比较跨数据集的 x
和 y
之间关系的形状。
我试图找出最有效的方法来做到这一点。现在我正在考虑创建一个空的ggplot对象,然后使用某种循环或 lapply
顺序添加到该对象,但这比我想象的要困难得多。当然,将模型简单地提供给 ggplot
是最容易的,但据我所知,这是不可能的。任何想法?
下面是一个简单的示例数据集,可以使用两个模型,一个线性和一个指数:
$ b $ (x = rnorm(10),y = rnorm(10))
df2 = data.frame(x = rnorm(15), y = rnorm(15))
df.list = list(lm(y_x,df1),nls(y_exp(a + b * x),start = list(a = 1 ,b = 1),df2))
以及两个单独的示例图:
ggplot(df1,aes(x,y))+ stat_smooth(method = lm,se = F)
ggplot(df2,aes x,y))+ stat_smooth(method = nls,formula = y_exp(a + b * x),start = list(a = 1,b = 1),se = F)
我想这里的答案是获得想要运行的X和Y的常见范围这结束了,并从那里开始。您可以使用预测从每个模型中拉出曲线,并使用l_ply将图层添加到ggplot。
d
f1 = data.frame(x = rnorm 10),y = rnorm(10))
df2 = data.frame(x = rnorm(15),y = rnorm(15))
df.list = list (a + b * x),start = list(a = 1,b = 1),df2))
a< -ggplot()
#获得你想看的x的范围
x
#使用l_ply继续添加图层
l_ply(df.list,function(amod){
#a预测变量和响应的数据帧
ndf< - data.frame(x = x)
#使用预测得到响应 - 你甚至可以得到一个CI这里
ndf $ y< - 预测(amod,ndf)
#现在将这个新图层添加到绘图
a<< - a + geom_line(ndf,mapping =(aes(x = x,y = y)))
})
a
或者,如果您想要使用型号或其他颜色的钥匙,可以使用不同的颜色键:
names(df.list)< - 1:length(df.list)
modFits< - ldply(df.list,function(amo d){
ndf< - data.frame(x = x)
#使用predict得到响应 - 你甚至可以在这里得到一个CI
ndf $ y< - 预测(amod,ndf)
ndf
})
qplot(x,y,geom =line, color = .id,data = modFits)
I have a list of linear and non-linear models derived from different data sets measuring the same two variables x
and y
that I would like to plot on the same plot using stat_smooth
. This is to be able to easily compare the shape of the relationship between x
and y
across datasets.
I'm trying to figure out the most effective way to do this. Right now I am considering creating an empty ggplot object and then using some kind of loop or lapply
to add sequentially to that object, but this is proving more difficult than I thought. Of course it would be easiest to simply supply the models to ggplot
but as far as I know, this is not possible. Any thoughts?
Here is a simple example data set to play with using just two models, one linear and one exponential:
df1=data.frame(x=rnorm(10),y=rnorm(10))
df2=data.frame(x=rnorm(15),y=rnorm(15))
df.list=list(lm(y~x,df1),nls(y~exp(a+b*x),start=list(a=1,b=1),df2))
And two separate example plots:
ggplot(df1,aes(x,y))+stat_smooth(method=lm,se=F)
ggplot(df2,aes(x,y))+stat_smooth(method=nls,formula=y~exp(a+b*x),start=list(a=1,b=1),se=F)
I think the answer here is to get a common range of X and Y you want to run this over, and go from there. You can pull out a curve from each model using predict, and add on layers to a ggplot using l_ply.
d
f1=data.frame(x=rnorm(10),y=rnorm(10))
df2=data.frame(x=rnorm(15),y=rnorm(15))
df.list=list(lm(y~x,df1),nls(y~exp(a+b*x),start=list(a=1,b=1),df2))
a<-ggplot()
#get the range of x you want to look at
x<-seq(min(c(df1$x, df2$x)), max(c(df1$x, df2$x)), .01)
#use l_ply to keep adding layers
l_ply(df.list, function(amod){
#a data frame for predictors and response
ndf <- data.frame(x=x)
#get the response using predict - you can even get a CI here
ndf$y <- predict(amod, ndf)
#now add this new layer to the plot
a<<- a+geom_line(ndf, mapping=(aes(x=x, y=y)))
} )
a
OR, if you want to have a nice color key with model number or something:
names(df.list) <- 1:length(df.list)
modFits <- ldply(df.list, function(amod){
ndf <- data.frame(x=x)
#get the response using predict - you can even get a CI here
ndf$y <- predict(amod, ndf)
ndf
})
qplot(x, y, geom="line", colour=.id, data=modFits)
这篇关于ggplot2 - 在同一图上绘制多个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!