2017-07-03 91 views
-1

我有一个使用ggplot多边形的问题。我绘制了一些模型预测和观察结果(平均值和置信水平),并且我想绘制一个多边形作为我观察的置信水平。这是我的数据R errorbar和多边形与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="")) 

我要的是增加polygon我的观测数据,但只要我无法想出一个办法来增加geom_polygon我的代码。

由此得出的数字如下:

confidence level

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

polygon confidence level 使用polygon创建的信心水平,在平均中间

+0

我建议使用'geom_ribbon'和'geom_line'。 – Axeman

+0

你可以请建议一种方法来使用它们吗?主要的问题是我不知道该怎么把数据作为参数,而对于aes – GigaByte123

+0

你的示例数据给了我一个完全不同的情节。 (观察和预测是一样的) – Axeman

回答

1

使用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")) 

enter image description here

+0

谢谢你完美的工作。 – GigaByte123