Show Sidebar Hide Sidebar

3D Streamtube Plots in R

How to create streamtube plots with Plotly.

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')
## [1] '4.8.0'

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')

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

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="streamtube-basic")
chart_link

Starting Position and Segments

library(plotly)

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

p <- 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
  ) %>%
  layout(
    scene = list(
      aspectratio = list(
        x = 2,
        y = 1,
        z = 0.3
      )
    ),
    margin = list(
      t = 20, b = 20, l = 20, r = 20
    )
  )

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="streamtube-advanced")
chart_link

Reference

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

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.