为最小-最大范围创建 geom_ribbon

2023-12-02

给出以下数据:

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))

看起来像这样:

normal plot

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: better plot with range


首先计算最小值和最大值怎么样:

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

然后简单地绘制功能区:

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

ggplot2 ribbon example

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为最小-最大范围创建 geom_ribbon 的相关文章

随机推荐