R中的coefplot;更改CI线颜色

时间:2015-09-07 14:21:47

标签: r ggplot2 coefplot

您好我在r中使用coefplot函数绘制出广义线性模型的系数。我想将95%CI线的颜色更改为与50%CI线不同。对于95%和50%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

3 个答案:

答案 0 :(得分:6)

您可以创建自己的系数图表,以满足您的需求而不会有太多麻烦。这是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

答案 1 :(得分:1)

您无法在此轻松更改颜色。 不幸的是,这里的包不能访问设置颜色。

  1. 使用grid包或gTable更改凹凸颜色。这是肮脏的解决方案。它假设你知道如何在ggplot树对象(gpath)中导航
  2. 或者您向buildPlotting.lm 添加新的颜色参数。你应该重新编译包。

答案 2 :(得分:0)

我遇到了同样的问题。 如果您更改单个coefplot图层,则可以在ggplot2内解决此问题。

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