2015-09-07 163 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