1

Say I some data, d, and I fit nls models to two subsets of the data.

x<- seq(0,4,0.1)
y1<- (x*2 / (0.2 + x))
y1<- y1+rnorm(length(y1),0,0.2)
y2<- (x*3 / (0.2 + x))
y2<- y2+rnorm(length(y2),0,0.4)
d<-data.frame(x,y1,y2)

m.y1<-nls(y1~v*x/(k+x),start=list(v=1.9,k=0.19),data=d)
m.y2<-nls(y2~v*x/(k+x),start=list(v=2.9,k=0.19),data=d)

I then want to plot the fitted model regression line over data, and shade the prediction interval. I can do this with the package investr and get nice plots for each subset individually:

require(investr)
plotFit(m.y1,interval="prediction",ylim=c(0,3.5),pch=19,col.pred='light blue',shade=T)

enter image description here

 plotFit(m.y2,interval="prediction",ylim=c(0,3.5),pch=19,col.pred='pink',shade=T)

enter image description here

However, if I plot them together I have a problem. The shading of the second plot covers the points and shading of the first plot: enter image description here

1: How can I make sure the points on the first plot end up on top of the shading of the second plot?

2: How can I make the region where the shaded prediction intervals overlap a new color (like purple, or any fusion of the two colors that are overlapping)?

colin
  • 2,606
  • 4
  • 27
  • 57
  • Are you sure that these are the prediction intervals you want? It's not clear to me from a quick look at the documentation if those are for inverse prediction or for normal prediction. Anyway, for the first question: `points(y1 ~ x, data = d)`, For the second question you need semi-transparent colors and I believe they are not supported on most devices. You could re-implement the `plotFit` function with ggplot2 where you could simply specify `alpha = 0.3`. – Roland Oct 23 '15 at 15:24
  • @roland yes I am sure these are the prediction intervals I want. Can you present an example of how I would implement plotfit in ggplot2? – colin Oct 23 '15 at 15:47
  • I could, but it would be too big a time investment. – Roland Oct 23 '15 at 15:47
  • @roland oh dangggg. sounds like you don't have the skills! – colin Oct 23 '15 at 15:49
  • Nice try. It rather sounds to me like you don't have the skills. – Roland Oct 23 '15 at 15:52
  • @Roland I don't. That's why I posted a question on stackoverflow asking how to do it. – colin Oct 23 '15 at 16:00

1 Answers1

3

Use adjustcolor to add transparency like this:

plotFit(m.y1, interval = "prediction", ylim = c(0,3.5), pch = 19, 
        col.pred = adjustcolor("lightblue", 0.5), shade = TRUE)

par(new = TRUE)
plotFit(m.y2, interval = "prediction", ylim = c(0,3.5), pch = 19, 
       col.pred =  adjustcolor("light pink", 0.5), shade = TRUE)

Depending on what you want you can play around with the two transparency values (here both set to 0.5) and possibly make only one of them transparent.

screenshot

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341