为多个变量在 ggplot2 上的线图中添加阴影标准偏差
adding a shaded standard deviation to line plots on ggplot2 for multiple variables
我有一个数据框,其中包含来自 5 个不同模型的多个集合,因此有 5 列加上一个日期列,第二个数据框包含标准差。
我想在一个图中绘制所有内容,其中我有不同模型的每个均值的均值和阴影标准差。知道我如何在 ggplot 中做到这一点吗?
示例数据:
表示:
Means <- structure(list(M1 = c(0.146803, 0.1477525, 0.1465378, 0.1430386,
0.14315, 0.1407827, 0.1394645, 0.1389529, 0.1400275, 0.1375498
), M2 = c(0.09307112, 0.09162262, 0.09091183, 0.09075522, 0.09127082,
0.08992585, 0.08821484, 0.08810128, 0.08770718, 0.08705453),
M3 = c(0.1310087, 0.1255959, 0.1271953, 0.1270623, 0.1242448,
0.1249174, 0.1247585, 0.1224901, 0.1228224, 0.1207565), M4 = c(0.1328935,
0.133063, 0.1302629, 0.1291629, 0.1255703, 0.1244377, 0.1235587,
0.1236105, 0.1206313, 0.1192216), M5 = c(0.1312402, 0.1296496,
0.1288667, 0.1304318, 0.1291016, 0.1290919, 0.1286337, 0.1284389,
0.1270611, 0.1289673), date = c("2015-01-01", "2016-01-01",
"2017-01-01", "2018-01-01", "2019-01-01", "2020-01-01", "2021-01-01",
"2022-01-01", "2023-01-01", "2024-01-01")), row.names = c(NA,
-10L), class = "data.frame")
标清:
SD <- structure(list(M1 = c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN), M2 = c(0.002352747, 0.002636449, 0.002584647, 0.002942681,
0.003041309, 0.00279849, 0.00282362, 0.002546572, 0.002362555,
0.003004829), M3 = c(0.003872364, 0.003809441, 0.003494403, 0.004341524,
0.00372956, 0.00382587, 0.00394011, 0.00428747, 0.0030507, 0.003493746
), M4 = c(0.002779382, 0.003130044, 0.003052774, 0.002544359,
0.0028259, 0.002732643, 0.001902435, 0.001727357, 0.002808552,
0.001431315), M5 = c(0.003038877, 0.004208446, 0.005034087, 0.003276497,
0.004041488, 0.004525613, 0.003653864, 0.00377299, 0.003307351,
0.00320737), date = c("2015-01-01", "2016-01-01", "2017-01-01",
"2018-01-01", "2019-01-01", "2020-01-01", "2021-01-01", "2022-01-01",
"2023-01-01", "2024-01-01")), row.names = c(NA, -10L), class = "data.frame")
df2 <- melt(Means, id = "date")
tit <- sprintf("%s %s Anuual Burden - %s", regnm, spcname, scenm)
filename <- sprintf("%s/TS_%s_%s_BurdenANN_%s.png",folderout, regnm, spcname, scenm)
png(filename,width = 8 * 360, height = 5 * 360, res = 360)
print(ggplot(data = df2, aes(x = date, y = value, color = variable)) +
geom_line(data=subset(df2, variable=="M1"), size=2) +
geom_line(data=subset(df2, variable=="M2"), size=2) +
geom_line(data=subset(df2, variable=="M3"), size=2) +
geom_line(data=subset(df2, variable=="M4"), size=2) +
geom_line(data=subset(df2, variable=="M5"), size=2) +
scale_colour_manual(name = spcname, breaks = c("M1","M2","M3","M4","M5"), values = clr2) +
xlab("Years") + ylab(sprintf("%s (Tg)", spcname)) + ggtitle(tit) + theme_bw() + theme(legend.key = element_blank()) +
guides(color = guide_legend(override.aes = list(linetype = c(1,1,1,1,1), shape = c(NA,NA,NA,NA,NA)))) + theme(plot.margin=unit(c(1,3,1,1),"cm"))+
theme(legend.position=c(1.1,.6), legend.direction = "vertical") +
theme(legend.title = element_blank())) # + expand_limits(y=0)
dev.off()
通过将数据帧转换为长格式并加入一个 df,这可以通过一个 geom_line
和一个 geom_ribbon
来实现,如下所示:
library(ggplot2)
library(dplyr)
library(tidyr)
means_long <- pivot_longer(Means, -date, values_to = "mean", names_to = "variable")
sd_long <- pivot_longer(SD, -date, values_to = "sd", names_to = "variable")
df_join <- means_long %>%
left_join(sd_long)
#> Joining, by = c("date", "variable")
ggplot(data = df_join, aes(x = date, group = variable)) +
geom_line(aes(y = mean, color = variable), size = 1) +
geom_ribbon(aes(y = mean, ymin = mean - sd, ymax = mean + sd, fill = variable), alpha = .2) +
xlab("Years") +
theme_bw() +
theme(legend.key = element_blank()) +
theme(plot.margin=unit(c(1,3,1,1),"cm"))+
theme(legend.position = c(1.1,.6), legend.direction = "vertical") +
theme(legend.title = element_blank())
#> Warning in max(ids, na.rm = TRUE): kein nicht-fehlendes Argument für max; gebe -
#> Inf zurück
由 reprex package (v0.3.0)
于 2020-05-20 创建
我有一个数据框,其中包含来自 5 个不同模型的多个集合,因此有 5 列加上一个日期列,第二个数据框包含标准差。 我想在一个图中绘制所有内容,其中我有不同模型的每个均值的均值和阴影标准差。知道我如何在 ggplot 中做到这一点吗?
示例数据:
表示:
Means <- structure(list(M1 = c(0.146803, 0.1477525, 0.1465378, 0.1430386,
0.14315, 0.1407827, 0.1394645, 0.1389529, 0.1400275, 0.1375498
), M2 = c(0.09307112, 0.09162262, 0.09091183, 0.09075522, 0.09127082,
0.08992585, 0.08821484, 0.08810128, 0.08770718, 0.08705453),
M3 = c(0.1310087, 0.1255959, 0.1271953, 0.1270623, 0.1242448,
0.1249174, 0.1247585, 0.1224901, 0.1228224, 0.1207565), M4 = c(0.1328935,
0.133063, 0.1302629, 0.1291629, 0.1255703, 0.1244377, 0.1235587,
0.1236105, 0.1206313, 0.1192216), M5 = c(0.1312402, 0.1296496,
0.1288667, 0.1304318, 0.1291016, 0.1290919, 0.1286337, 0.1284389,
0.1270611, 0.1289673), date = c("2015-01-01", "2016-01-01",
"2017-01-01", "2018-01-01", "2019-01-01", "2020-01-01", "2021-01-01",
"2022-01-01", "2023-01-01", "2024-01-01")), row.names = c(NA,
-10L), class = "data.frame")
标清:
SD <- structure(list(M1 = c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN,
NaN, NaN), M2 = c(0.002352747, 0.002636449, 0.002584647, 0.002942681,
0.003041309, 0.00279849, 0.00282362, 0.002546572, 0.002362555,
0.003004829), M3 = c(0.003872364, 0.003809441, 0.003494403, 0.004341524,
0.00372956, 0.00382587, 0.00394011, 0.00428747, 0.0030507, 0.003493746
), M4 = c(0.002779382, 0.003130044, 0.003052774, 0.002544359,
0.0028259, 0.002732643, 0.001902435, 0.001727357, 0.002808552,
0.001431315), M5 = c(0.003038877, 0.004208446, 0.005034087, 0.003276497,
0.004041488, 0.004525613, 0.003653864, 0.00377299, 0.003307351,
0.00320737), date = c("2015-01-01", "2016-01-01", "2017-01-01",
"2018-01-01", "2019-01-01", "2020-01-01", "2021-01-01", "2022-01-01",
"2023-01-01", "2024-01-01")), row.names = c(NA, -10L), class = "data.frame")
df2 <- melt(Means, id = "date")
tit <- sprintf("%s %s Anuual Burden - %s", regnm, spcname, scenm)
filename <- sprintf("%s/TS_%s_%s_BurdenANN_%s.png",folderout, regnm, spcname, scenm)
png(filename,width = 8 * 360, height = 5 * 360, res = 360)
print(ggplot(data = df2, aes(x = date, y = value, color = variable)) +
geom_line(data=subset(df2, variable=="M1"), size=2) +
geom_line(data=subset(df2, variable=="M2"), size=2) +
geom_line(data=subset(df2, variable=="M3"), size=2) +
geom_line(data=subset(df2, variable=="M4"), size=2) +
geom_line(data=subset(df2, variable=="M5"), size=2) +
scale_colour_manual(name = spcname, breaks = c("M1","M2","M3","M4","M5"), values = clr2) +
xlab("Years") + ylab(sprintf("%s (Tg)", spcname)) + ggtitle(tit) + theme_bw() + theme(legend.key = element_blank()) +
guides(color = guide_legend(override.aes = list(linetype = c(1,1,1,1,1), shape = c(NA,NA,NA,NA,NA)))) + theme(plot.margin=unit(c(1,3,1,1),"cm"))+
theme(legend.position=c(1.1,.6), legend.direction = "vertical") +
theme(legend.title = element_blank())) # + expand_limits(y=0)
dev.off()
通过将数据帧转换为长格式并加入一个 df,这可以通过一个 geom_line
和一个 geom_ribbon
来实现,如下所示:
library(ggplot2)
library(dplyr)
library(tidyr)
means_long <- pivot_longer(Means, -date, values_to = "mean", names_to = "variable")
sd_long <- pivot_longer(SD, -date, values_to = "sd", names_to = "variable")
df_join <- means_long %>%
left_join(sd_long)
#> Joining, by = c("date", "variable")
ggplot(data = df_join, aes(x = date, group = variable)) +
geom_line(aes(y = mean, color = variable), size = 1) +
geom_ribbon(aes(y = mean, ymin = mean - sd, ymax = mean + sd, fill = variable), alpha = .2) +
xlab("Years") +
theme_bw() +
theme(legend.key = element_blank()) +
theme(plot.margin=unit(c(1,3,1,1),"cm"))+
theme(legend.position = c(1.1,.6), legend.direction = "vertical") +
theme(legend.title = element_blank())
#> Warning in max(ids, na.rm = TRUE): kein nicht-fehlendes Argument für max; gebe -
#> Inf zurück
由 reprex package (v0.3.0)
于 2020-05-20 创建