2015-09-07 158 views
3

嗨我在r中使用coefplot函数来绘制广义线性模型的系数。我想改变95%CI线的颜色与50%CI线的颜色不同。 95%和50%CI线的颜色参数默认为相同的颜色。coefplot in R;改变CI线的颜色

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

回答

5

您或许可以创建自己的系数图的版本,以满足您的需求而不会有太多麻烦。这里有一个ggplot2例如:

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

+0

另一点是使用'coefplot'输出,而不是你自己的系数。 'mm < - coefplot(model1,plot = FALSE)'然后你使用''HighInner“,”LowInner“,”HighOuter'..'变量,也许你可以把它作为练习。 – agstudy

1

你不能轻易改变这里的颜色。 不幸的是,这个软件包不允许访问这里设置的颜色。

  1. 要么使用grid包或gTable更改grobs颜色。这是肮脏的解决方案。它假定你知道一点点如何在ggplot树对象(gpath)中导航
  2. 或者你可以给buildPlotting.lm添加一个新的颜色参数。你应该重新编译这个包。
+0

感谢agstudy。开始怀疑你提到的东西,但首先要确认。 –

0

我有同样的问题。 如果您更改个别的ggplot2图层,则可以在coefplot之内解决该问题。

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