One of the courses I teach is a degree module which includes an introduction to solving ordinary differential equations (ODEs) numerically using
R. I make heavy use of the deSolve package and normally plot results with ggplot2 but one of the models we look at is a 2/3 body problem and we generate results in 3d. At the moment we’ve been using scatterplot3d as it’s pretty easy to use but it is kind of lacking something when it comes to helping students see what is happening. You can change the angle you want to look at it but need to redraw the plot each time - which brings me onto plotly.
Recently I’ve been using it in some shiny apps and rather than using
ggplotly(), plotting in it directly 1. This led me to look at the documentation a little more closely and discover how simple it is to produce a 3d scatterplot.
If we model a 3-body problem where one body moves directly upwards and the other two rotate around it then the results end of in the long form shown below.
body x y z 1 one 0 0 0.000 2 one 0 0 0.025 3 one 0 0 0.050 4 one 0 0 0.075 5 one 0 0 0.100 6 one 0 0 0.125
Producing the plot then requires a relatively small amount on code.
library(plotly) plot_ly(results, x = ~x, y = ~y, z = ~z, color = ~body, colors = "Set1") %>% add_markers() %>% layout(title = "Three Body System")
The plot can be rotated to see it from different perspectives and the tooltips automatically provide the coordinates of the points, all of which does seem to lend it a little more to enabling students to see what is happening. I will try it out the next time I teach the module and see what the feedback is. In the meantime I will continue to use plotly.
As an aside, it drastically speeded up plotting in a shiny app.↩