3

嗨,我在 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() 

在此处输入图像描述

4

3 回答 3

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()

在此处输入图像描述

于 2015-09-07T18:37:00.800 回答
1

你不能轻易改变这里的颜色。不幸的是,该软件包无法在此处访问设置颜色。

  1. 您可以使用gridpackage 或gTable. 这是肮脏的解决方案。它假设您知道一点如何在 ggplot 树对象 ( gpath) 中导航
  2. 或者您向buildPlotting.lm添加一个新的颜色参数。你应该重新编译这个包。
于 2015-09-07T15:00:25.353 回答
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 图像

于 2018-01-05T11:46:09.433 回答