我對函式 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)

uj5u.com熱心網友回復:
也許這就是你正在尋找的。猜你要為每個模型,你可以映射置信帶testOn的x和group通過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
)

資料
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"
))
``
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/368353.html
上一篇:如何在JavaScript中使用localStorage存盤背景顏色的變化?
下一篇:如何用參考線覆寫箱線圖
