Jesse001 Jesse001 - 9 days ago 8
R Question

How do I change the colors of the confidence interval lines in the matline function?

I'm plotting a logarithmic regression's line of best fit as well as the confidence intervals around that line. The code I'm using works well enough, except I'd rather that the confidence intervals both be gray (rather than the default red and green). Unfortunately, I'm not seeing a way to isolate them when specifying color changes.

Ideally I'd like it to end with the regression line itself to have: lty=1, col="black", and the confidence intervals to have: lty=2, col="gray".

How can I isolate the confidence interval lines to make them gray and change their type independent of the regression line itself?

my code is of the sort:

R6cl<-lm(log(R6$y)~R6$x)
pR6cl<-predict(R6cl,interval="confidence")
plot(R6$x,log(R6$y), type= "p")
matlines(x=R6$x,y=log(R6$y),lwd=2,lty=1, col="black")


which produces:

enter image description here

Answer

How about:

a = 1:10
b = c(2,1,2,4,5,5,3,7,4,10)

R6cl <- lm(log(b)~a)
pR6cl <- predict(R6cl, interval = "confidence")
plot(a, log(b), type = "p") 
lines(a, pR6cl[,1], lty = 1, col = "black")
lines(a, pR6cl[,2], lty = 2, col = "gray")
lines(a, pR6cl[,3], lty = 2, col = "gray")

Which gives:

plot

Comments