3

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

Anand Roopsind
  • 581
  • 2
  • 8
  • 11

3 Answers3

6

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

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • 1
    Another point is to use the `coefplot` output and not your own coefficients. `mm <- coefplot(model1,plot=FALSE)` then you use `"HighInner", "LowInner", "HighOuter`..` variables. Maybe you can keep this to the OP as an exercise. – agstudy Sep 07 '15 at 21:22
1

You can't easily change the color here. Unfortunately the package don't give access to set colors here.

  1. Either you change the grobs colors using grid package or gTable. This is the dirty solution. It assume you know a little bit how to navigate in the ggplot tree objects ( gpath)
  2. Or you add a new color parameter to buildPlotting.lm . You should recompile the package.
agstudy
  • 119,832
  • 17
  • 199
  • 261
0

I had the same problem. You can solve it within coefplot if you alter the individual ggplot2 layers.

coef <- coefplot(model = model1
     , color = "blue"
     ) +
      theme_bw() 
coef$layers[[2]] <- geom_errorbarh(stat="identity", position = "identity", na.rm = FALSE
                                    , color = "yellow"
                                    , size = 1
                                    , mapping = aes(xmin = LowOuter, xmax = HighOuter
                                                    , height = 0, linetype = Model 
                                    ))
coef$layers[[3]] <- geom_errorbarh(stat="identity", position = "identity", na.rm = FALSE
                                    , color = "red"
                                    , size = 2
                                    , mapping = aes(xmin = LowInner, xmax = HighInner
                                          , height = 0, linetype = Model
                                          ))
coef

coefplot image

Dom R
  • 1
  • 2