r - Creating "spaghetti" longitudinal plot with smooting / jitter -
i trying create spaghetti plots out of data consisting of numbers 0 4, obtained @ 15 different time points. large data, cannot paste here, when trying
matplot(x,y,type="l",lty=1,col="#00000020")
how can "smooth" lines , make them less overlapping, in lower plot? if @ possible, without using ggplot2.
just smooth data in number of ways, some examples
nr <- 200 mm <- t(matrix(sample(0:4, nr * 15, replace = true), nr)) set.seed(1) mm[sample(length(mm), nr * 15 / 20)] <- na x <- 1:15 par(mfrow = c(1,2)) matplot(mm, type = 'l', lty = 1, xlim = c(0,15), ylim = c(-5,10), col = adjustcolor('black', alpha.f = .1)) plot('mm', xlim = c(0,15), ylim = c(-5,10), panel.last = grid(), bty = 'l') (ii in 1:ncol(mm)) { dd <- data.frame(y = mm[, ii], x = x) lo <- loess(y ~ x, data = dd, na.action = 'na.omit') # lo <- loess(mm[, ii] ~ x) xl <- seq(min(x), max(x), (max(x) - min(x)) / 1000) lines(xl, predict(lo, xl), col = adjustcolor('black', alpha.f = .1)) }
Comments
Post a Comment