Home > OS >  R - geom_ribbon() with not unique x-values
R - geom_ribbon() with not unique x-values

Time:11-28

I have a problem with the function geom_ribbon (R).

My dataset has not unique x values (i.e. vertical lines on the plot). How can I set the correct value of aes(x) of the function geom_ribbon?

This is the dataset:

DF
# A tibble: 12 x 5
# Groups:   model [3]
   model                  testOn      mean   lci   uci
   <fct>                  <chr>      <dbl> <dbl> <dbl>
 1 1 - trained up to 2005 2006-01-01 0.503 0.495 0.510
 2 1 - trained up to 2005 2007-01-01 0.514 0.507 0.521
 3 2 - trained up to 2006 2007-01-01 0.505 0.501 0.510
 4 2 - trained up to 2006 2008-01-01 0.501 0.496 0.506
 5 2 - trained up to 2006 2009-01-01 0.501 0.497 0.506
 6 2 - trained up to 2006 2010-01-01 0.504 0.498 0.510
 7 2 - trained up to 2006 2011-01-01 0.499 0.495 0.503
 8 6 - trained up to 2010 2011-01-01 0.505 0.498 0.511
 9 6 - trained up to 2010 2012-01-01 0.504 0.498 0.510
10 6 - trained up to 2010 2013-01-01 0.503 0.499 0.508
11 6 - trained up to 2010 2014-01-01 0.503 0.497 0.509
12 6 - trained up to 2010 2015-01-01 0.504 0.492 0.516

This is the plot function:

    ggplot(data = DF, 
           aes(y = mean, x = testOn, color=model)) 
      geom_line(aes(group=1)) 
      geom_point(size = 1.3) 
      geom_ribbon(aes(x= 1:length(testOn), ymin=lci, ymax=uci), 
                   alpha = 0.3, show.legend = FALSE)

enter image description here

CodePudding user response:

Maybe this is what you are looking for. Guessing that you want confidence bands for each model you could map testOn on x and group by model:

library(ggplot2)

ggplot(
  data = DF,
  aes(y = mean, x = testOn, color = model)
)  
  geom_line(aes(group = 1))  
  geom_point(size = 1.3)  
  geom_ribbon(aes(x = testOn, ymin = lci, ymax = uci, group = model),
    alpha = 0.3, show.legend = FALSE
  )

DATA

DF <- structure(list(model = c(
  1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
  3L, 3L, 3L
), testOn = c(
  "2006-01-01", "2007-01-01", "2007-01-01",
  "2008-01-01", "2009-01-01", "2010-01-01", "2011-01-01", "2011-01-01",
  "2012-01-01", "2013-01-01", "2014-01-01", "2015-01-01"
), mean = c(
  0.503,
  0.514, 0.505, 0.501, 0.501, 0.504, 0.499, 0.505, 0.504, 0.503,
  0.503, 0.504
), lci = c(
  0.495, 0.507, 0.501, 0.496, 0.497, 0.498,
  0.495, 0.498, 0.498, 0.499, 0.497, 0.492
), uci = c(
  0.51, 0.521,
  0.51, 0.506, 0.506, 0.51, 0.503, 0.511, 0.51, 0.508, 0.509, 0.516
)), class = "data.frame", row.names = c(
  "1", "2", "3", "4", "5",
  "6", "7", "8", "9", "10", "11", "12"
))
``
  • Related