为最小-最大范围创建geom_ribbon [英] Create geom_ribbon for min-max range

查看:145
本文介绍了为最小-最大范围创建geom_ribbon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下数据:

df<-data.frame(
  year=(1996:2000),
  a=c(2,1.5,1.5,2,3),
  b=c(2,2,2,3,4),
  c=c(2,3,3,1,1))

使用ggplot:

  ggplot(df,aes(x=year))+
    geom_line(aes(y=a))+
    geom_line(aes(y=b))+
    geom_line(aes(y=c))

看起来像这样:

如何.我可以创建一个始终显示最小值和最大值之间的区域的功能区(将其视为最小-最大范围),如下所示:

how. can I create a ribbon that shows always the area between minimum and maximum (think of it like a min-max-range), to get it look like this:

推荐答案

首先计算最小值和最大值怎么办?

What about first computing the mininum and maximum values:

df$min <- apply(df[, -1], 1, min)
df$max <- apply(df[, -1], 1, max)

然后简单地绘制功能区:

And then simply plotting the ribbon:

ggplot(df, aes(x = year, ymin = min, ymax = max)) + geom_ribbon()

这篇关于为最小-最大范围创建geom_ribbon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆