Carpet Contour Plot in R

How to create Carpet Contour Plots in R 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.

Basic Carpet Plot

Set the x and y coorindates, using x and y attributes. If x coorindate values are ommitted a cheater plot will be created. To save parameter values use a and b attributes. To make changes to the axes, use aaxis or baxis attributes. For a more detailed list of axes attributes refer to R reference.

library(plotly)

fig <- plot_ly()
fig <- fig %>% add_trace(
    type = 'carpet',
    a = c(0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3),
    b = c(4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6),
    x = c(2, 3, 4, 5, 2.2, 3.1, 4.1, 5.1, 1.5, 2.5, 3.5, 4.5),
    y = c(1, 1.4, 1.6, 1.75, 2, 2.5, 2.7, 2.75, 3, 3.5, 3.7, 3.75),
    xaxis = "x",
    yaxis = "y",
    carpet = "c",
    aaxis = list(
      tickprefix = "a = ",
      smoothing = 0,
      minorgridcount = 9,
      type = 'linear'
    ),
    baxis = list(
      tickprefix = "b = ",
      smoothing = 0,
      minorgridcount = 9,
      type = 'linear'
    )
  )
fig <- fig %>% layout(
    margin = list(
      t = 40, r = 30, b = 30, l = 30
    ),
    yaxis = list(
      range = c(0.388,4.361)
    ),
    xaxis = list(
      range = c(0.667,5.932)
    )
  )

fig

Add Contours

library(plotly)

fig <- plot_ly(width = 600, height = 600)
fig <- fig %>% add_trace(
    type = 'contourcarpet',
    a = c(0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3),
    b = c(4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6),
    z = c(1, 1.96, 2.56, 3.0625, 4, 5.0625, 1, 7.5625, 9, 12.25, 15.21, 14.0625),
    xaxis = "x",
    yaxis = "y",
    carpet = "c",
    autocontour = F,
    contours = list(
      start = 1,
      end = 14,
      size = 1
    ),
    line = list(
      width = 2,
      smoothing = 0
    ),
    colorbar = list(
      len = 0.4,
      y = 0.25
    )
  )
fig <- fig %>% add_trace(
    type = 'carpet',
    a = c(0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3),
    b = c(4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6),
    x = c(2, 3, 4, 5, 2.2, 3.1, 4.1, 5.1, 1.5, 2.5, 3.5, 4.5),
    y = c(1, 1.4, 1.6, 1.75, 2, 2.5, 2.7, 2.75, 3, 3.5, 3.7, 3.75),
    xaxis = "x",
    yaxis = "y",
    carpet = "c",
    aaxis = list(
      tickprefix = "a = ",
      smoothing = 0,
      minorgridcount = 9,
      type = 'linear'
    ),
    baxis = list(
      tickprefix = "b = ",
      smoothing = 0,
      minorgridcount = 9,
      type = 'linear'
    )
  )
fig <- fig %>% layout(
    margin = list(
      t = 40, r = 30, b = 30, l = 30
    ),
    yaxis = list(
      range = c(0.388,4.361)
    ),
    xaxis = list(
      range = c(0.667,5.932)
    )
  )

fig

Add Multiple Traces

library(plotly)
library(rjson)

data <- fromJSON(file="https://raw.githubusercontent.com/bcdunbar/datasets/master/airfoil_data.json")

fig <- plot_ly() 
fig <- fig %>%
  add_trace(
    type = "carpet",
    a = list(
      1.083, 1.214, 1.344, 1.475, 1.605, 1.736, 1.866, 1.997, 2.128, 2.258, 2.389, 2.519, 2.650, 2.780, 2.911, 3.041, 3.172, 3.303, 3.433, 3.564, 3.694, 3.825, 3.955, 4.086, 4.217, 4.347, 4.478, 4.608, 4.739, 4.869, 5.000
    ),
    b = list(
      0.000, 0.090, 0.180, 0.269, 0.359, 0.449, 0.539, 0.628, 0.718, 0.808, 0.898, 0.987, 1.077, 1.167, 1.257, 1.346, 1.436, 1.526, 1.616, 1.705, 1.795, 1.885, 1.975, 2.064, 2.154, 2.244, 2.334, 2.424, 2.513, 2.603, 2.693,
      2.783, 2.872, 2.962, 3.052, 3.142, 3.231, 3.321, 3.411, 3.501, 3.590, 3.680, 3.770, 3.860, 3.949, 4.039, 4.129, 4.219, 4.308, 4.398, 4.488, 4.578, 4.668, 4.757, 4.847, 4.937, 5.027, 5.116, 5.206, 5.296, 5.386, 5.475,
      5.565, 5.655, 5.745, 5.834, 5.924, 6.014, 6.104, 6.193, 6.283
    ),
    x = data[[1]]$x,
    y = data[[1]]$y,
    aaxis = list(
      startlinewidth = 2,
      startline = TRUE,
      showticklabels = "none",
      endline = TRUE,
      showgrid = FALSE,
      endlinewidth = 2,
      smoothing = 0
    ),
    baxis = list(
      startline = FALSE,
      endline = FALSE,
      showticklabels = "none",
      smoothing = 0,
      showgrid = FALSE
    )
  )
fig <- fig %>% add_trace(
      type = 'contourcarpet',
      z = data[[2]]$z,
      autocolorscale = FALSE,
      zmax = 1,
      name = "Pressure",
      colorscale = "Viridis",
      zmin = -8,
      colorbar = list(
        y = 0,
        yanchor = "bottom",
        titleside = "right",
        len = 0.75,
        title = "Pressure coefficient, c<sub>p</sub>"
      ),
      contours = list(
        start = -1,
        size = 0.025,
        end = 1.000,
        showlines = FALSE
      ),
      line = list(
        smoothing = 0
      ),
      autocontour = FALSE,
      zauto = FALSE
    )
fig <- fig %>% add_trace(
      type = 'contourcarpet',
      z = data[[3]]$z,
      opacity = 0.300,
      showlegend = TRUE,
      name = "Streamlines",
      autocontour = TRUE,
      ncontours = 50,
      contours = list(
        coloring = "none"
      ),
      line = list(
        color = "white",
        width = 1
      )
    )
fig <- fig %>% add_trace(
      type = 'contourcarpet',
      z = data[[4]]$z,
      showlegend = TRUE,
      name = "Pressure<br>contours",
      autocontour = FALSE,
      line = list(
        color = "rgba(0, 0, 0, 0.5)",
        smoothing = 1
      ),
      contours = list(
        size = 0.250,
        start = -4,
        coloring = "none",
        end = 1.000,
        showlines = TRUE
      )
    )
fig <- fig %>% add_trace(
      type = 'scatter',
      x = data[[5]]$x,
      y = data[[5]]$y,
      legendgroup = "g1",
      name = "Surface<br>pressure",
      mode = "lines",
      hoverinfo = "skip",
      line = list(
        color = "rgba(255, 0, 0, 0.5)",
        width = 1,
        shape = "spline",
        smoothing = 1
      ),
      fill = "toself",
      fillcolor = "rgba(255, 0, 0, 0.2)"
    )
fig <- fig %>% add_trace(
      type = 'scatter',
      x = data[[6]]$x,
      y = data[[6]]$y,
      showlegend = FALSE,
      legendgroup = "g1",
      mode = "lines",
      hoverinfo = "skip",
      line = list(
        color = "rgba(255, 0, 0, 0.3)",
        width = 1
      )
    )
fig <- fig %>% add_trace(
      type = 'scatter',
      x = data[[7]]$x,
      y = data[[7]]$y,
      showlegend = FALSE,
      legendgroup = "g1",
      name = "cp",
      mode = "lines",
      line = list(
        color = "rgba(255, 0, 0, 0.2)",
        width = 0
      )
    )
fig <- fig %>% layout(
      xaxis = list(
        zeroline = FALSE,
        scaleratio = 1,
        scaleanchor = 'y',
        range = c(-3.800,3.800),
        showgrid = FALSE
      ),
      yaxis = list(
        zeroline = FALSE,
        range = c(-1.800,1.800),
        showgrid = FALSE
      ),
      title = "Flow over a Karman-Trefftz airfoil",
      hovermode = "closest",
      margin = list(
        r = 60,
        b = 40,
        l = 40,
        t = 80
      )
    )

fig

Reference

See https://plotly.com/r/reference/#contourcarpet for more information and 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)