【问题标题】:R errorbar and polygon with ggplot带有ggplot的R误差线和多边形
【发布时间】:2017-07-03 15:39:47
【问题描述】:

我在使用带有 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_ribbongeom_line
  • 你能推荐一种使用它们的方法吗?主要问题是我不知道该为数据和 aes 提供什么参数
  • 您的示例数据给了我一个完全不同的情节。 (观察和预测是一样的)
  • 我已经修改了数据,请您考虑更新一下,现在应该可以了。

标签: r ggplot2 polygon


【解决方案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"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    相关资源
    最近更新 更多