R errorbar 和多边形与 ggplot

R errorbar and polygon with ggplot

我在将多边形与 ggplot 结合使用时遇到问题。我绘制了一些模型预测和观察结果(平均值和置信水平),并且我想为我的观察结果的置信水平绘制一个多边形。这是我的数据

Mat_ic_mu=matrix(c(-3.347582, -3.297287, -3.333237, -3.206484, -3.313200,
-3.313200,-3.313200,-3.313200,-3.355346, -3.315213, -3.354656, -3.252734,
-3.328607, -3.328607, -3.328607, -3.328607,-3.363109, -3.333138,
-3.376076, -3.298983, -3.344014, -3.344014, -3.344014,
-3.344014),ncol=3,nrow=8)

modelName=c("model 1","model 2","model 3","model 0")
type=rep(c("Pred modèle", "Obs"), each = 4)
boxdata=data.frame(Mat_ic_mu,modelName,type)
colnames(boxdata)=c("icp","pred","icm","model","type")

ggplot(boxdata, aes(x = model, y = pred, ymax = icp, ymin = icm, 
                    group = type, colour = type, shape = type)) +
  geom_errorbar(width=0.20) +
  geom_point() +
  scale_shape_manual(name="legend",values=c(19, 4)) +
  scale_color_manual(name="legend",values = c("orange","deepskyblue")) +
  xlab("modèles") + 
  ylab("intervalle de confiance")+
  ggtitle(paste("Intervalle de confiance en ",end_year+1," pour ",Pays_a_predire," âge " , age_bp+start_age-1,sep=""))

我想要的是为我的 Obs 添加 polygon,但是很长一段时间我都想不出一种方法来将 geom_polygon 添加到我的代码中。

结果图如下:

但我真正想要的是更像这样的东西:

使用 polygon 创建置信度,平均值在中间

使用 geom_linegeom_ribbon,我们需要将数据子集化到我们的层:

ggplot(mapping = aes(x = model, y = pred, ymax = icp, ymin = icm)) +
  geom_ribbon(aes(group = 1), data = subset(boxdata, type == "Obs"), alpha = 0.2) +
  geom_line(aes(group = 1), data = subset(boxdata, type == "Obs")) +
  geom_errorbar(data = subset(boxdata, type == "Pred modèle"), width = 0.20) +
  geom_point(data = subset(boxdata, type == "Pred modèle"))