使用现有行作为 geom_ribbon() 最小值和最大值
Using existing lines as geom_ribbon() min and max
所以我绘制了三条线。上下线是置信区间,所以我想用 geom_ribbon() 用颜色填充线之间的区域。但我一直无法弄清楚。我认为下面的代码清楚地说明了我要完成的工作,但这不起作用并提供了以下错误:"Discrete value supplied to continuous scale."
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
以及三行图表:
我还被要求提供以下输出:
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")
如有任何指导,我们将不胜感激。
对于这样的图,您需要将数据框重新整形为宽格式,因为 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)
所以我绘制了三条线。上下线是置信区间,所以我想用 geom_ribbon() 用颜色填充线之间的区域。但我一直无法弄清楚。我认为下面的代码清楚地说明了我要完成的工作,但这不起作用并提供了以下错误:"Discrete value supplied to continuous scale."
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
以及三行图表:
我还被要求提供以下输出:
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")
如有任何指导,我们将不胜感激。
对于这样的图,您需要将数据框重新整形为宽格式,因为 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)