Search code examples
rggplot2coefplot

coefplot in R; change CI line colours


Hi I'm using coefplot function in r to plot out the coefficients from a generalized linear model. I would like to change the colour of the 95% CI lines to be different from the 50% CI lines. The colour parameters defaults the same colour for both the 95% and 50% CI lines.

coeff<-coefplot(model1,pointSize=5,color="black",fillColor="grey",lwdOuter = 1.2,lwdInner=2)

coeff + theme_bw() +
  theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank()) +
  theme (axis.title.y  = element_text(size=16)) +
  theme(axis.title.x = element_text(size=16)) +
  scale_y_discrete(name="",labels=c("NDAA","GAP","SS","PS","LL")) +
  theme (axis.text.x  = element_text(size=16)) +
  theme(axis.text.x = element_text(size=16)) +
  scale_x_continuous(name="Regression Estimate") +
  labs(title = "") +
  coord_flip() 

enter image description here


Solution

  • You could probably create your own version of a coefficient plot that meets your needs without too much trouble. Here's a ggplot2 example:

    library(ggplot2)
    
    # Create a model to plot
    m1 = lm(mpg ~ wt + cyl + carb, data=mtcars)
    coefs = as.data.frame(summary(m1)$coefficients[-1,1:2])
    names(coefs)[2] = "se" 
    coefs$vars = rownames(coefs)
    
    ggplot(coefs, aes(vars, Estimate)) + 
      geom_hline(yintercept=0, lty=2, lwd=1, colour="grey50") +
      geom_errorbar(aes(ymin=Estimate - 1.96*se, ymax=Estimate + 1.96*se), 
                    lwd=1, colour="red", width=0) +
      geom_errorbar(aes(ymin=Estimate - se, ymax=Estimate + se), 
                    lwd=2.5, colour="blue", width=0) +
      geom_point(size=4, pch=21, fill="yellow") +
      theme_bw()
    

    enter image description here