简体   繁体   English

ggplot2-在同一图上绘制多个模型

[英]ggplot2 - plot multiple models on the same plot

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 . 我有一个线性和非线性模型的列表,这些模型是从不同的数据集派生而来的,这些数据集测量的两个变量xy是我想使用stat_smooth在同一图上stat_smooth This is to be able to easily compare the shape of the relationship between x and y across datasets. 这样可以轻松比较整个数据集之间xy之间关系的形状。

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. 现在,我正在考虑创建一个空的ggplot对象,然后使用某种循环或lapply依次向该对象添加对象,但是事实证明,这比我想象的要困难得多。 Of course it would be easiest to simply supply the models to ggplot but as far as I know, this is not possible. 当然,简单地将模型提供给ggplot是最容易的,但是据我所知,这是不可能的。 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)

EDIT: Note that the OP changed the question after this answer was posted 编辑:请注意,此答案发布后,OP更改了问题

Combine the data into a single data frame, with a new column indicating the model, then use ggplot to distinguish between the models: 将数据合并到一个数据框中,并用新列指示模型,然后使用ggplot区分模型:

df1=data.frame(x=rnorm(10),y=rnorm(10))
df2=data.frame(x=rnorm(10),y=rnorm(10))

df1$model <- "A"
df2$model <- "B"

dfc <- rbind(df1, df2)

library(ggplot2)
ggplot(dfc, aes(x, y, group=model)) + geom_point() + stat_smooth(aes(col=model))

This produces: 这将产生:

在此处输入图片说明

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. 我认为,这里的答案是得到一个想要运行的X和Y的通用范围,然后从那里开始。 You can pull out a curve from each model using predict, and add on layers to a ggplot using l_ply. 您可以使用预测从每个模型中绘制一条曲线,并使用l_ply将图层添加到ggplot中。

d 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM