在R中,如何用ggvis绘制多条曲线?不知道怎么做,我试着用for循环来做,
m <- 10
n_max <- 100
n <- seq.int(1, n_max, 1)
s <- matrix(2*(rbinom(n_max*m, size=1, prob=0.5) - 0.5), ncol = m)
x_estimate <- apply(s, 2, function(z) cumsum(z)/seq_along(z))
data <- data.frame(x_estimate, n)
v <- data %>%
ggvis(x = ~n, y = ~x_estimate[,c(1)]) %>%
layer_lines() %>%
scale_numeric("y", domain = c(-0.5, 0.5), nice = FALSE, clamp = TRUE)
for ( i in 2:m) {
v <- (v %>% layer_paths(x = ~n, y = ~x_estimate[,c(i)]))
}
v
结果图只有 2 条曲线,而不是 m=10 条曲线。因此,for 循环以某种方式绘制重叠曲线。有任何想法吗?更重要的是,如何正确地做到这一点ggvis
?