簡體   English   中英

在 ggplot2 上為多個變量的線圖添加陰影標准偏差

[英]adding a shaded standard deviation to line plots on ggplot2 for multiple variables

我有一個數據框,其中包含來自 5 個不同模型的多個集合,因此 5 列加上一個日期列,以及我有標准偏差的第二個數據框。 我想將 plot 全部放在一個 plot 中,其中我對不同模型的每個平均值都有平均值和陰影標准偏差。 知道如何在 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 年 5 月 20 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM