Ggplot2 - build multiple models on the same site

I have a list of linear and non-linear models obtained from different data sets measuring the same two variables x and y that I would like to build on the same plot using stat_smooth . This makes it easy to compare the relationship between x and y through data sets.

I am trying to find the most efficient way to do this. Right now, I am considering creating an empty ggplot object, and then using some kind of loop or lapply to sequentially add to this object, but this turns out to be more complicated than I thought. Of course, it would be easier to just put the models on ggplot , but as far as I know, this is not possible. Any thoughts?

Here is a simple example of a data set for playback using only 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 examples:

 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) 
+4
source share
2 answers

I think the answer here is to get the general range of X and Y that you want to run and go from there. You can draw a curve from each model using the forecast and add layers to 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 a 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) 
+3
source

EDIT: Please note that the OP changed the question after posting this answer

Combine the data into one data frame with a new column designating the model, then use ggplot to distinguish between the models:

 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 gives:

enter image description here

+9
source

Source: https://habr.com/ru/post/1444884/


All Articles