Trajectory Plot inside of 3D transparent sphere using R -
i want make trajectory plot of df
inside of transparent 3d sphere.
i searched stackoverflow couldn't find same question. might helpful interested trajectory of vectors.
the df
this
df <- data.frame(mx=runif(100,-0.05,0.05), my=runif(100,-1,1), mz=runif(100,-0.5,0.5))
i agree frank's answer. if want plot trajectories on sphere supplied picture, should bit more careful, since ordinary interpolation won't give paths on sphere. there different options, easiest project paths onto sphere.
require(rgl) # construct brownian motion on sphere n <- 100 sigma <- 0.5 df <- array(na, dim = c(n, 3)) df[1, ] <- rnorm(3, sd = sigma) # starting point df[1, ] <- df[1, ] / sqrt(sum(df[1, ]^2)) (i in 2:n) { df[i, ] <- rnorm(3, sd = sigma) + df[i - 1, ] df[i, ] <- df[i, ] / sqrt(sum(df[i, ]^2)) } # linear interpolation of observed trajectories, projected onto sphere times <- seq(1, n, length = 1000) xx <- approx(1:n, df[, 1], xout = times)$y yy <- approx(1:n, df[, 2], xout = times)$y zz <- approx(1:n, df[, 3], xout = times)$y df_proj <- cbind(xx, yy, zz) df_proj <- df_proj / sqrt(rowsums(df_proj ^2)) # plot plot3d(df_proj, type = 'l', col = heat.colors(1000), lwd = 2, xlab = 'x', ylab = 'y', zlab = 'z') rgl.spheres(0, 0, 0, radius = 0.99, col = 'red', alpha = 0.6, = 'lines')
you can of course same thing smooth trajectories frank's answer:
# smooth trajectories plot times <- seq(1, n, length = 1000) xx <- spline(1:n, df[, 1], xout = times)$y yy <- spline(1:n, df[, 2], xout = times)$y zz <- spline(1:n, df[, 3], xout = times)$y df_smooth <- cbind(xx, yy, zz) df_smooth <- df_smooth / sqrt(rowsums(df_smooth^2)) plot3d(df_smooth, type = 'l', col = heat.colors(1000), lwd = 2, xlab = 'x', ylab = 'y', zlab = 'z') rgl.spheres(0, 0, 0, radius = 0.99, col = 'red', alpha = 0.6, = 'lines')
Comments
Post a Comment