25-75 percentile shading area

Dear R users,

I plotted one graph with 5 year running mean line by

  geom_line(aes(y=rollmean(DJF, 5, na.pad=TRUE)),size=0.8)  


Now, I want to draw a 25-75 percentile shading area over the running mean line.

Any idea how can I do that?

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

The data can found on

and the code which I am using it:


  ggplot(data = DJF_SM01, aes(x = year, y = DJF, group =  scenario, color=scenario))+ 
  geom_line(alpha=0.3) +
  scale_shape(solid=FALSE) +
  xlab("Year") +
  ylim(0,0.15)+
  labs(title= "RunningMean=5year") +
  theme(legend.title=element_blank()) +
  theme(legend.text=element_text(size=rel(1.1)))+
  theme(legend.position="bottom")+
  theme(axis.title = element_text(face="plain",size=13,color="black"),
        axis.text=element_text(size=10,face="plain", color="black"),
        plot.title = element_text(size=12)) +
  geom_line(aes(y=rollmean(DJF, 5, na.pad=TRUE)),size=0.8)
library(tidyverse)
library(zoo)
DJF_SM01 <- structure(list(year = c(
  1930, 1931, 1932, 1933, 1934, 1935, 1936,
  1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947,
  1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958,
  1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969,
  1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980,
  1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991,
  1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
  2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
  2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024,
  2025, 2026, 2027, 2028, 2029, 2030, 2031, 2032, 2033, 2034, 2035,
  2036, 2037, 2038, 2039, 2040, 2041, 2042, 2043, 2044, 2045, 2046,
  2047, 2048, 2049, 2050, 2051, 2052, 2053, 2054, 2055, 2056, 2057,
  2058, 2059, 2060, 2061, 2062, 2063, 2064, 2065, 2066, 2067, 2068,
  2069, 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077, 2078, 2079,
  2080, 2081, 2082, 2083, 2084, 2085, 2086, 2087, 2088, 2089, 2090,
  2091, 2092, 2093, 2094, 2095, 2096, 2097, 2098, 2099, 2100, 2015,
  2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026,
  2027, 2028, 2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037,
  2038, 2039, 2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048,
  2049, 2050, 2051, 2052, 2053, 2054, 2055, 2056, 2057, 2058, 2059,
  2060, 2061, 2062, 2063, 2064, 2065, 2066, 2067, 2068, 2069, 2070,
  2071, 2072, 2073, 2074, 2075, 2076, 2077, 2078, 2079, 2080, 2081,
  2082, 2083, 2084, 2085, 2086, 2087, 2088, 2089, 2090, 2091, 2092,
  2093, 2094, 2095, 2096, 2097, 2098, 2099, 2100, 2015, 2016, 2017,
  2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028,
  2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039,
  2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050,
  2051, 2052, 2053, 2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061,
  2062, 2063, 2064, 2065, 2066, 2067, 2068, 2069, 2070, 2071, 2072,
  2073, 2074, 2075, 2076, 2077, 2078, 2079, 2080, 2081, 2082, 2083,
  2084, 2085, 2086, 2087, 2088, 2089, 2090, 2091, 2092, 2093, 2094,
  2095, 2096, 2097, 2098, 2099, 2100, 2015, 2016, 2017, 2018, 2019,
  2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030,
  2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041,
  2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050, 2051, 2052,
  2053, 2054, 2055, 2056, 2057, 2058, 2059, 2060, 2061, 2062, 2063,
  2064, 2065, 2066, 2067, 2068, 2069, 2070, 2071, 2072, 2073, 2074,
  2075, 2076, 2077, 2078, 2079, 2080, 2081, 2082, 2083, 2084, 2085,
  2086, 2087, 2088, 2089, 2090, 2091, 2092, 2093, 2094, 2095, 2096,
  2097, 2098, 2099, 2100
), DJF = c(
  0.0094292, 0.017211, 0.0072534,
  0.014323, 0.022546, 0.012165, 0.01502, 0.015019, 0.022855, 0.011905,
  0.0088874, 0.017038, 0.011206, 0.022627, 0.011391, 0.016473,
  0.016863, 0.011335, 0.0090494, 0.011175, 0.019059, 0.01269, 0.010048,
  0.011512, 0.024775, 0.015032, 0.0082941, 0.011718, 0.016022,
  0.013789, 0.010523, 0.0081917, 0.031476, 0.0392, 0.018211, 0.013305,
  0.028173, 0.014156, 0.012776, 0.015095, 0.017525, 0.00939, 0.014389,
  0.018333, 0.015593, 0.013993, 0.0073861, 0.0061219, 0.014598,
  0.014424, 0.013204, 0.0066971, 0.021616, 0.024245, 0.017364,
  0.015257, 0.037386, 0.022977, 0.011297, 0.013977, 0.017938, 0.013396,
  0.0093861, 0.023366, 0.020419, 0.019106, 0.033798, 0.0191, 0.015507,
  0.024009, 0.021049, 0.020859, 0.046552, 0.012224, 0.01434, 0.027803,
  0.020136, 0.028005, 0.025207, 0.051418, 0.015975, 0.021385, 0.043098,
  0.030814, 0.031614, 0.038297, 0.035663, 0.032587, 0.015439, 0.015412,
  0.013214, 0.02593, 0.029986, 0.034838, 0.016052, 0.046042, 0.030548,
  0.037427, 0.039859, 0.055428, 0.020346, 0.036774, 0.030678, 0.01479,
  0.017187, 0.027981, 0.029679, 0.019909, 0.029399, 0.039904, 0.031983,
  0.024811, 0.029894, 0.023608, 0.017867, 0.038715, 0.050001, 0.030389,
  0.020185, 0.017284, 0.017372, 0.015191, 0.024146, 0.016129, 0.025197,
  0.022814, 0.025901, 0.038716, 0.04045, 0.029139, 0.036918, 0.032513,
  0.025438, 0.023659, 0.024313, 0.027215, 0.020181, 0.031551, 0.022278,
  0.025747, 0.027649, 0.026433, 0.021237, 0.022892, 0.029074, 0.033278,
  0.035477, 0.027924, 0.028368, 0.046369, 0.015105, 0.023871, 0.020131,
  0.032204, 0.021696, 0.026246, 0.017688, 0.019023, 0.01901, 0.023679,
  0.033231, 0.020692, 0.027281, 0.022264, 0.04476, 0.011873, 0.015914,
  0.024283, 0.057495, 0.024686, 0.014486, 0.033907, 0.054888, 0.044157,
  0.038852, 0.036337, 0.035953, 0.020104, 0.021479, 0.028933, 0.032576,
  0.027332, 0.047133, 0.0475, 0.03477, 0.026388, 0.035094, 0.046726,
  0.026797, 0.029478, 0.045981, 0.032319, 0.037158, 0.044585, 0.039234,
  0.032451, 0.031282, 0.020027, 0.037049, 0.018731, 0.02399, 0.034358,
  0.046505, 0.027521, 0.037918, 0.01021, 0.02452, 0.02364, 0.036271,
  0.040939, 0.035195, 0.047437, 0.041359, 0.031021, 0.041761, 0.045051,
  0.025339, 0.045798, 0.039469, 0.029943, 0.029085, 0.038348, 0.040479,
  0.056792, 0.039053, 0.069959, 0.030782, 0.034357, 0.037885, 0.048922,
  0.038192, 0.054392, 0.060477, 0.056118, 0.047833, 0.03446, 0.032434,
  0.029263, 0.032977, 0.028546, 0.026436, 0.027634, 0.037274, 0.061912,
  0.033075, 0.047506, 0.021598, 0.043754, 0.034172, 0.048789, 0.059224,
  0.030449, 0.045808, 0.065122, 0.082231, 0.038711, 0.018185, 0.050616,
  0.015097, 0.053627, 0.034708, 0.036609, 0.028216, 0.03602, 0.024027,
  0.022857, 0.030736, 0.048699, 0.043849, 0.032764, 0.02067, 0.063959,
  0.04, 0.017121, 0.040192, 0.035545, 0.050379, 0.021522, 0.036047,
  0.023742, 0.03685, 0.057092, 0.03401, 0.060304, 0.02712, 0.024001,
  0.023067, 0.059348, 0.053497, 0.048739, 0.038149, 0.022789, 0.026572,
  0.034304, 0.037214, 0.032229, 0.041555, 0.069943, 0.028071, 0.077436,
  0.047901, 0.028794, 0.040764, 0.083193, 0.10328, 0.047603, 0.098146,
  0.060912, 0.041284, 0.032787, 0.08164, 0.062585, 0.058575, 0.039512,
  0.070838, 0.076537, 0.057299, 0.045301, 0.053532, 0.082517, 0.083438,
  0.043317, 0.10008, 0.055344, 0.042745, 0.11343, 0.07768, 0.043422,
  0.057791, 0.043129, 0.062303, 0.047699, 0.055902, 0.084361, 0.091678,
  0.061435, 0.07584, 0.085587, 0.10822, 0.10673, 0.11587, 0.08329,
  0.10552, 0.045174, 0.020082, 0.042289, 0.035736, 0.051334, 0.065426,
  0.026853, 0.03118, 0.037649, 0.036991, 0.023834, 0.026565, 0.03052,
  0.024843, 0.041276, 0.03196, 0.030807, 0.047781, 0.028505, 0.03426,
  0.016896, 0.045489, 0.044875, 0.043099, 0.03269, 0.02577, 0.032045,
  0.043706, 0.063305, 0.033755, 0.049106, 0.034087, 0.026583, 0.019128,
  0.026011, 0.038998, 0.033626, 0.052886, 0.019408, 0.03313, 0.031098,
  0.035784, 0.029778, 0.041171, 0.048592, 0.057278, 0.047047, 0.046659,
  0.032718, 0.069432, 0.033572, 0.044326, 0.035241, 0.062422, 0.039659,
  0.046676, 0.071494, 0.057712, 0.063842, 0.084124, 0.053744, 0.052124,
  0.042312, 0.050409, 0.076753, 0.065023, 0.054687, 0.066875, 0.1358,
  0.058792, 0.1196, 0.062585, 0.10145, 0.091934, 0.079277, 0.072287,
  0.095036, 0.11628, 0.072014, 0.10585, 0.070518, 0.13823, 0.12572,
  0.093608, 0.12126, 0.099231
), scenario = structure(c(
  1L, 1L,
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
  4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
  4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
  4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
  5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
  5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
  5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
  5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
  5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L
), .Label = c(
  "hist",
  "ssp126", "ssp245", "ssp370", "ssp585"
), class = "factor")), row.names = c(
  NA,
  -429L
), spec = structure(list(cols = list(year = structure(list(), class = c(
  "collector_double",
  "collector"
)), DJF = structure(list(), class = c(
  "collector_double",
  "collector"
)), scenario = structure(list(), class = c(
  "collector_character",
  "collector"
))), default = structure(list(), class = c(
  "collector_guess",
  "collector"
)), skip = 1), class = "col_spec"), class = c(
  "spec_tbl_df",
  "tbl_df", "tbl", "data.frame"
))

smrydjf <- DJF_SM01 %>% group_by(scenario) %>% 
  summarise(q1 = quantile(DJF,probs = c(0.25)),
            q2 = quantile(DJF,probs = c(0.75))) %>% ungroup 
smrydjf2 <- left_join(smrydjf,
                      DJF_SM01,
                      by="scenario")


ggplot(data = smrydjf2, aes(x = year, y = DJF, group = scenario, color = scenario)) +
  geom_ribbon(mapping=aes(ymin=q1,ymax=q2,fill=scenario),alpha=.2)+
  geom_line(alpha = 0.3) +
  scale_shape(solid = FALSE) +
  xlab("Year") +
  ylim(0, 0.15) +
  labs(title = "RunningMean=5year") +
  theme(legend.title = element_blank()) +
  theme(legend.text = element_text(size = rel(1.1))) +
  theme(legend.position = "bottom") +
  theme(
    axis.title = element_text(face = "plain", size = 13, color = "black"),
    axis.text = element_text(size = 10, face = "plain", color = "black"),
    plot.title = element_text(size = 12)
  ) +
  geom_line(aes(y = rollmean(DJF, 5, na.pad = TRUE)), size = 0.8) 


  geom_hline(data=smrydjf,
              mapping=aes(color=scenario,
                          yintercept=q1),linetype="dashed")+
  geom_hline(data=smrydjf,
              mapping=aes(color=scenario,
                          yintercept=q2),linetype="dashed")

Thank you so much for spending some time on my issue.

But this figure sees line incorrect in terms of showing shading area. I wanted to draw shading area around my lines, not in horizontal way. I tried by using

 geom_ribbon(aes(y=DJF, ymax=DJF+p75, ymin=DJF-p25), alpha=0.2)+

but didn't work.

I dont thinknyour data supports doing anything else. What values are you wanting the 75th and 25th percentiles of if not scenario?. You don't have multiple data per year within scenario after all....

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.