R: ggplot2を使ってみる(2) [統計]
散布図の次は折れ線グラフ。ランダムウォークの軌跡を表示してみる。
library(ggplot2) N <- 61 M <- 3 x <- rep(seq(0, (N - 1), 1), M) y <- vector("numeric", N * M) r <- runif(N * M, 0, 1) g <- rep(1:M, each = N) for (j in 1:M) { y[1 + (j - 1) * N] <- 0 } for (j in 1:M) { for (i in 2:N) { t <- i + (j - 1) * N y[t] <- y[t - 1] + ifelse(r[t] > 0.5 , 1, -1) } } random.walk <- data.frame(x = x, y = y, trial = as.factor(g)) p <- ggplot(random.walk, aes(x = x, y = y, trial = trial)) p1 <- p + geom_line(aes(colour = trial)) max.y <- max(random.walk$y) min.y <- min(random.walk$y) max.y5 <- ((max.y - 0.1) %/% 5 + 1) * 5 min.y5 <- (min.y %/% 5) * 5 p2 <- p1 + scale_x_continuous(name = "time", limits = c(0, (N - 1)), breaks = seq(0, (N - 1), 5), minor_breaks = seq(0, (N - 1))) p3 <- p2 + scale_y_continuous(name = "location", limits = c(min.y5, max.y5), breaks = seq(min.y5, max.y5, 5), minor_breaks = seq(min.y5, max.y5)) print(p3)
結果
コメント 0