将现有行用作geom_ribbon()最小和最大

问题描述 投票:1回答:1

所以我要绘制三行。上下两行是置信区间,因此我想使用geom_ribbon()用颜色填充两行之间的区域。但是我还没弄清楚。我认为以下代码可以清楚地说明我要完成的工作,但这不起作用,并提供以下错误:“离散值提供给连续刻度。”

library(ggplot2)
library(gganimate)

rise3 %>% 
ggplot(aes(x=as.numeric(Year), 
           y=CM_increase,
           group=Scenario))+
geom_line(color="#134e13",
          size=1.25) +
geom_ribbon(aes(x=as.numeric(Year), 
                ymax=Scenario == "1.5 - MED", 
                ymin=Scenario == "0.5 - MED")) +
transition_reveal(as.numeric(Year)) +
theme_hc()

这是数据集的外观

    Site           ID   Latitude Longitude Scenario RSL_rate Year  CM_increase
1   SEWELLS POINT   299 36.95   -76.33  0.5 - MED   2.47    2000    0
2   SEWELLS POINT   299 36.95   -76.33  1.0 - MED   2.47    2000    0
3   SEWELLS POINT   299 36.95   -76.33  1.5 - MED   2.47    2000    0
4   SEWELLS POINT   299 36.95   -76.33  0.5 - MED   2.47    2010    7

以及三行图表:enter image description here

我也被要求提供以下输出:

    dput(head(rise3, 20))
structure(list(Site = c("SEWELLS POINT", "SEWELLS POINT", "SEWELLS POINT", 
"SEWELLS POINT", "SEWELLS POINT", "SEWELLS POINT", "SEWELLS POINT", 
"SEWELLS POINT", "SEWELLS POINT", "SEWELLS POINT", "SEWELLS POINT", 
"SEWELLS POINT", "SEWELLS POINT", "SEWELLS POINT", "SEWELLS POINT", 
"SEWELLS POINT", "SEWELLS POINT", "SEWELLS POINT", "SEWELLS POINT", 
"SEWELLS POINT"), `PSMSL ID` = c(299L, 299L, 299L, 299L, 299L, 
299L, 299L, 299L, 299L, 299L, 299L, 299L, 299L, 299L, 299L, 299L, 
299L, 299L, 299L, 299L), Latitude = c(36.95, 36.95, 36.95, 36.95, 
36.95, 36.95, 36.95, 36.95, 36.95, 36.95, 36.95, 36.95, 36.95, 
36.95, 36.95, 36.95, 36.95, 36.95, 36.95, 36.95), Longitude = c(-76.33, 
-76.33, -76.33, -76.33, -76.33, -76.33, -76.33, -76.33, -76.33, 
-76.33, -76.33, -76.33, -76.33, -76.33, -76.33, -76.33, -76.33, 
-76.33, -76.33, -76.33), Scenario = c("0.5 - MED", "1.0 - MED", 
"1.5 - MED", "0.5 - MED", "1.0 - MED", "1.5 - MED", "0.5 - MED", 
"1.0 - MED", "1.5 - MED", "0.5 - MED", "1.0 - MED", "1.5 - MED", 
"0.5 - MED", "1.0 - MED", "1.5 - MED", "0.5 - MED", "1.0 - MED", 
"1.5 - MED", "0.5 - MED", "1.0 - MED"), `Background RSL rate (mm/yr)` = c(2.47, 
2.47, 2.47, 2.47, 2.47, 2.47, 2.47, 2.47, 2.47, 2.47, 2.47, 2.47, 
2.47, 2.47, 2.47, 2.47, 2.47, 2.47, 2.47, 2.47), Year = c("2000", 
"2000", "2000", "2010", "2010", "2010", "2020", "2020", "2020", 
"2030", "2030", "2030", "2040", "2040", "2040", "2050", "2050", 
"2050", "2060", "2060"), CM_increase = c(0L, 0L, 0L, 7L, 9L, 
11L, 15L, 19L, 24L, 22L, 30L, 38L, 30L, 42L, 54L, 37L, 55L, 73L, 
45L, 70L)), row.names = c(NA, 20L), class = "data.frame")

任何指导将不胜感激。

r ggplot2 confidence-interval gganimate
1个回答
3
投票

对于这种绘图,您需要将数据框重整为宽格式,因为ggplot要求每个麻醉都使用单独的列。

library(tidyr)
library(ggplot2)

rise3_wide <- spread(rise3, Scenario, CM_increase)

ggplot(rise3_wide, aes(x = as.numeric(Year))) +
  geom_line(aes(y = `1.0 - MED`)) +
  geom_ribbon(aes(ymax = `1.5 - MED`, ymin = `0.5 - MED`),
              fill = "green", alpha = .2)

© www.soinside.com 2019 - 2024. All rights reserved.