Overlaying multiple stat_function calls in ggplot2

I have two raw and coef data frames:

  • one containing raw data
  • another containing simulation coefficients that I got from raw data.

The first raw data frame contains:

  • Time (0 to 900 seconds)
  • OD for many options and four runs.

The second coef data frame contains:

  • one line per combination of options / t0.1 with separate coefficients ( M , D.1 and t0.1 ) in this line.

I runID separation of the raw data by options and colored by runID , no problem. But now I want to impose model curves according to runID .

Since simulation coefficients are in different data frames with different dimensions, I cannot just cbind them. stat_function will not work for me. I can only get one curve showing at a time.

I tried with for loop , adding a stat_function layer every time:

 p <- ggplot(temp, aes(Time, OD)) + geom_point(aes(colour = runID), size = 2) #works fine! calc <- function(x){temp.n$M[ID] * (1 - exp(temp.n$D.1[ID] * temp.n$t0.1[ID] - x)))} for(ID in 1:length(unique(temp.n$runID))) { p <- p + stat_function(fun = calc) } print(p) 

At the end, all p returns this plot of the source data and the final curve from the cycle bit. p seems to be reset every time I try to add a new stat_function layer.

Any ideas?

+9
function r plot ggplot2 overlay
source share
2 answers

Following the solution given here , you may have to imitate the stat_function effect yourself. Since you are not providing a reproducible example, I created a simple example that we hope will mimic your problem:

 library(ggplot2) reg.fun <- function(x, par1, par2){exp(-x*par1) + par2} #functional form reg <- data.frame(g=factor(1:3), par1=(1:3)/10, par2=1:3) #parameters for 3 groups #generate data from reg.fun dd <- expand.grid(x=0:9, g=reg$g) #set x values, and 3 groups from reg dd <- merge(dd, reg) #"import" parameters dd$mn <- with(dd, reg.fun(x, par1, par2)) #value of function for given x's dd$y <- rnorm(30, mean=dd$mn, sd=0.5) #add variability dd <- subset(dd, select=c(g,x,y)) #remove auxiliary variables #similarly to above generate values for the function on a fine grid of x values pred.dd <- expand.grid(x=seq(0,9, length=101), g=levels(dd$g)) pred.dd <- merge(pred.dd, reg) pred.dd$y <- with(pred.dd, reg.fun(x, par1, par2)) #draw the plot p <- qplot(x,y, colour=g, data=dd) #scatterplot of data p + geom_line(data=pred.dd) #add the curves of the functions 
+3
source share

I had the same problem with you. In a very un-elegant solution, the only solution I found was to hack the stat functions together as follows:

 p <- ggplot(temp, aes(Time, OD)) + geom_point(aes(colour = runID), size = 2) #works fine! calc <- function(x){temp.n$M[ID] * (1 - exp(temp.n$D.1[ID] * temp.n$t0.1[ID] - x)))} p <- p + stat_function(fun = function(x){temp.n$M[1] * (1 - exp(temp.n$D.1[1] * temp.n$t0.1[1] - x)))) + stat_function(fun = function(x){temp.n$M[2] * (1 - exp(temp.n$D.1[2] * temp.n$t0.1[2] - x)))) + stat_function(fun = function(x){temp.n$M[3] * (1 - exp(temp.n$D.1[3] * temp.n$t0.1[3] - x)))) + # etc 

This is fine if you only have a few lines to add, but not if you have many.

+1
source share

All Articles