Search code examples
rplot3dline

How to plot several graphs one after another


I would like to plot my graphs in one 3d coordinate system. I it possible to plot these graphs in series (one after another) without defining a z coordinate.

x = 460:650
y1 = rnorm(191, 5, 2)
y2 = rnorm(191, 4, 1)
y3 = rnorm(191, 2, 0.8)

plot(x, y1, col = "red", type = "l")
lines(x, y2, col = "green", add = T)
lines(x, y3, col = "blue", add = T)

Here is an example data set. At the moment I plot my graphs all in the same 2d coordinate system. I want them in the same 3d coordinate system. Meaning the red curve should be the foremost and the blue curve the last curve. All these 3 plots can have the same distance from each other.


Solution

  • This is all types of ugly but, I think it does what you want. Adapted from https://plot.ly/r/3d-line-plots/.

    library(plotly) 
    x = rep(460:650,3) 
    y1 = rnorm(191, 5, 2)
    y2 = rnorm(191, 4, 1)
    y3 = rnorm(191, 3, 1)
    z = c(rep(1,191),rep(2,191),rep(3,191))
    z <- ordered(z, levels=c('1', '2', '3'))
    
    y_all<-(c(y1,y2,y3))
    plot_ly(x = ~x, y = ~y_all, z = ~z, type = 'scatter3d', mode = 'lines', color = ~z)
    

    As others mentioned, no you cannot plot in z-space without defining some z. Here, you just need a categorical z to set things against and then you can tell plot_ly() to format it "nicely"