Show Sidebar Hide Sidebar

WebGL Text and Annotations in R

How to add WebGL text labels and annotations to plots in R.

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'

Heatmap with Annotations

library(plotly)
library(random)

n=250
t=12
x = c(rep(0:(n-1), times=t))
y = c(rep(0:(t-1), each=n))
z = randomNumbers(n=n*t, min=1, max=10, col=n)
text = c(replicate(t*n, sample(c("A","T","G","C"), 1)))

steps = list()
for (e in c(0:(n-30))){
  step <- list(
    list(
      args = list('xaxis.range', c(-0.5 + e, 30.5 + e)),
      method = "relayout",
      label = e,
      value = e
    )
  )
  steps[e] <- step
}

p <- plot_ly() %>%
  add_trace(
    type='heatmap',
    z = z
  ) %>%
  add_trace(
    mode = "text",
    text = text,
    type = "scattergl",
    textfont = list(
      size = 20
    ),
    x = x,
    y = y
  ) %>%
  layout(
    xaxis = list(
      range = c(-0.5, 30.5),
      showline = F,
      zeroline = F,
      showgrid = F
    ),
    yaxis = list(
      showline = F,
      zeroline = F,
      showgrid = F
    ),
    sliders=list(
      list(
        active = 0,
        steps = steps
      )
    )
  )

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

Reference

See https://plot.ly/r/reference/#scattergl for more information and chart attribute options!

Still need help?
Contact Us

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