您需要 3 列,时间戳、值和标准。就像在 aes(ymin, ymax) 中使用 std 列一样简单:
library(tidyverse)
huron <- data.frame(year = 1875:1972,
value = LakeHuron,
std = runif(length(LakeHuron),0,1))
huron %>%
ggplot(aes(year, value)) +
geom_ribbon(aes(ymin = value - std, ymax = value + std), fill = "steelblue2") +
geom_line(color = "firebrick", size = 1)
如果您想对数据进行分组,您应该在 aes 中使用 fill = your_group 和 group = your_group:
library(tidyverse)
huron <- data.frame(year = rep(1875:1972,2),
group = c(rep("a",98),rep("b",98)),
value = c(LakeHuron, LakeHuron + 5),
std = runif(length(LakeHuron)*2,0,1))
huron %>%
ggplot(aes(year, value, fill = group, group=group)) +
geom_ribbon(aes(ymin = value - std, ymax = value + std), fill =
"steelblue2") +
geom_line(color = "firebrick", size = 1)
我在此处发布了此提示:https://typethepipe.com/vizs-and-tips/ggplot_geom_ribbon_shadow/ 了解更多信息。希望对您有所帮助!