在 R 中使用 dwplot 保留置信区间,同时在 R 中设置 x 轴限制

Retaining confidence intervals with dwplot in R while setting x axis limits in R

我正在尝试使用 R 中的 ggplot2 和 dotwhisker 包来显示模型的系数估计值。我的一个协变量具有非常宽的置信区间,因此 x 轴的尺度非常大。当我将 x 轴限制设置为比 CI 更窄时,CI 条从图中消失。有没有办法保留 CIs,即使它们将被裁剪?

比如我生成图1:

data(mtcars)
mtest <- lm(mpg ~ cyl + hp + wt, data = mtcars)
dwplot(mtest) 

当我尝试设置轴限制时,我丢失了 CI,如图 2 所示:

dwplot(mtest) +  scale_x_continuous(limits = c(-4, 1))

谢谢!

可能有更优雅的解决方案,但解决方法是检查置信区间并手动绘制胡须。

data(mtcars)
mtest <- lm(mpg ~ cyl + hp + wt, data = mtcars)
summary(mtest)
confint(mtest)
dwplot(mtest) +  scale_x_continuous(expand = c(0,0), limits = c(-4, 1))+
  geom_segment(aes(x=-4, xend=-1.649972, y=1, yend = 1), 
  color = "salmon")

这会生成以下图:

一种比手绘你自己的胡须要求(低得多)的方法是 coord_cartesian():

library(dotwhisker)
#> Loading required package: ggplot2

data(mtcars)
mtest <- lm(mpg ~ cyl + hp + wt, data = mtcars)
dwplot(mtest) + coord_cartesian(xlim = c(-4, 1))

reprex package (v2.0.1)

创建于 2022-01-18