R - geom_ribbon() 不具有唯一的 x 值

R - geom_ribbon() with not unique x-values

我的函数 geom_ribbon (R) 有问题。

我的数据集没有唯一的 x 值(即图中的垂直线)。 如何设置函数 geom_ribbon 的 aes(x) 的正确值?

这是数据集:

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

这是绘图函数:

    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)

也许这就是您要找的。猜测您想要每个模型的置信区间,您可以通过 model:

xgroup 上映射 testOn
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
  )

数据

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"
))
``