我试图用置信区间在同一图上绘制2个不同模型的参数估计值。每个参数的值都是重叠的,我希望每个x值(参数)的值都是并排的。
数据
ci_glm<- dput(head(ci_glm))
structure(list(parameter = c(-0.989960390831752, 0.23802371512626,
-0.0616305811832892, -1.19145279737722, -0.39565631764158, -2.70713419498971
), lower = c(-1.76343111098339, -0.0384145902617419, -0.338256948651047,
-2.41452042708909, -1.56076899496423, -3.8540190563328), upper = c(0.037528250100757,
0.514419550437131, 0.214814325315589, -0.0336965457336884, 0.639688082685478,
-1.70838885452134), par = c("Intercept", "Vessel 2", "Vessel 3",
"10", "11", "13")), .Names = c("parameter", "lower", "upper",
"par"), row.names = c("(Intercept)", "as.factor(CruiseID)201502",
"as.factor(CruiseID)201503", "as.factor(Stratum)10", "as.factor(Stratum)11",
"as.factor(Stratum)13"), class = "data.frame")
ci_boot<-dput(head(ci_boot2))
structure(list(parameter = c(-1.23409264614473, NA, NA, -0.434928403121171,
-2.74151010196932, -0.361626461606862), lower = c(-1.99928925205138,
NA, NA, -2.16613527555384, -1.38979210854727, -3.9529283095427
), upper = c(-0.118870916073164, NA, NA, 0.0238247660480798,
0.860847808652077, -1.44129237641604), par = c("Intercept", "Vessel 2",
"Vessel 3", "10", "11", "13"), diff = c(1.88041833597822, NA,
NA, 2.18996004160192, 2.25063991719935, 2.51163593312666), diff1 = c(0.76519660590665,
NA, NA, 1.73120687243267, -1.35171799342205, 3.59130184793584
), diff2 = c(1.11522173007157, NA, NA, 0.458753169169251, 3.6023579106214,
-1.07966591480918)), .Names = c("parameter", "lower", "upper",
"par", "diff", "diff1", "diff2"), row.names = c("(Intercept)",
"1", "11", "as.factor(Stratum)10", "as.factor(Stratum)11", "as.factor(Stratum)13"
), class = "data.frame")
积
require(plotrix)
plot(ci_glm$parameter, type='n', ylab="Parameter Estimate",xlab="Parameter",pch=20, axes=F,ylim=c(-4,4))
axis(1, at=1:6, labels=unique(ci_glm$par),las=3,cex.axis=0.8)
axis(2)
abline(h=0,lty=2,col="light gray")
plotCI(ci_glm$parameter,y=NULL,uiw = abs(ci_glm$upper-ci_glm$parameter),liw=abs(ci_glm$parameter-ci_glm$lower),
ui=NULL,li=NULL,err="y", sfrac=0.01,gap=0,slty=par("lty"),
add=T,scol="black",pch=18,pt.bg=par("bg",col="black"))
par(new=T)
plot(ci_boot$parameter, type='n',ylab="Parameter Estimate",xlab="Parameter",pch=20, axes=F,ylim=c(-4,4),col="red")
axis(2)
abline(h=0,lty=2,col="light gray")
plotCI(ci_boot$parameter,y=NULL,uiw = abs(ci_boot$upper-ci_boot$parameter),liw=abs(ci_boot$parameter-ci_boot$lower),
ui=NULL,li=NULL,err="y", sfrac=0.01,gap=0,slty=par("lty"),
add=T,scol="red",pch=18,pt.bg=par("bg",col="red"))
leg.text<-c("GLM","GLMM")
legend("bottomright",leg.text,lty=c(1,1),pch=c(20,20),text.col="black",col=c("black","red"),bty="y",cex=.8)
R version 3.3.1 (2016-06-21)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
答案 0 :(得分:1)
如果您愿意切换到使用ggplot2
,可以使用position_dodge()
轻松完成此操作,如此处所示。我也使用dplyr
将两个模型输出组合成一个plottable data.frame。
toPlot <-
bind_rows(boot = ci_boot %>% mutate(Parameter = row.names(.))
, glm = ci_glm %>% mutate(Parameter = row.names(.))
, .id = "Model")
ggplot(
toPlot
, aes(x = Parameter
, y = parameter
, ymin = lower
, ymax = upper
, col = Model)) +
geom_linerange(position = position_dodge(0.2)) +
geom_point(position = position_dodge(0.2)) +
theme(axis.text.x = element_text(angle = 45
, hjust = 1))
请注意,对于一些引导模型参数,估计值在某种程度上超出了置信区间的范围。您可能希望清理标签等(并删除或提供1
和11
的值),但这解决了重叠问题。