3D Streamtube Plots in R

How to create streamtube plots with Plotly.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Introduction

In streamtube plots, attributes inlcude x, y, and z, which set the coorindates of the vector field, and u, v, and w, which sets the x, y, and z components of the vector field. Additionally, you can use starts to determine the streamtube's starting position. Lastly, maxdisplayed determines the maximum segments displayed in a streamtube.

Basic Streamtube Plot

library(plotly)

df = read.csv('https://raw.githubusercontent.com/plotly/datasets/master/streamtube-basic.csv')

fig <- df %>%
  plot_ly(
    type = 'streamtube',
    x = ~x,
    y = ~y,
    z = ~z,
    u = ~u,
    v = ~v,
    w = ~w,
    sizeref = 0.5,
    cmin = 0,
    cmax = 3
  ) 
fig <- fig %>%
  layout(
    scene = list(
      camera = list(
        eye = list(
          x = -0.7243612458865182,
          y = 1.9269804254717962,
          z = 0.6704828299861716
        )
      )
    )
  )

fig

Starting Position and Segments

library(plotly)

df = read.csv('https://raw.githubusercontent.com/plotly/datasets/master/streamtube-wind.csv')

fig <- df %>%
  plot_ly(
    type = 'streamtube',
    x = ~x,
    y = ~y,
    z = ~z,
    u = ~u,
    v = ~v,
    w = ~w,
    starts = list(
      x = rep(80, 16),
      y = rep(c(20,30,40,50), 4),
      z = c(rep(0,4),rep(5,4),rep(10,4),rep(15,4))
    ),
    sizeref = 0.3,
    showscale = F,
    maxdisplayed = 3000
  ) 
fig <- fig %>%
  layout(
    scene = list(
      aspectratio = list(
        x = 2,
        y = 1,
        z = 0.3
      )
    ),
    margin = list(
      t = 20, b = 20, l = 20, r = 20
    )
  )

fig

Reference

See our reference page for more information and chart attribute options!.

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)