plotting nls fits with overlapping prediction inte

2019-05-30 23:31发布

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

1条回答
姐就是有狂的资本
2楼-- · 2019-05-31 00:18

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

查看更多
登录 后发表回答