|
| 1 | +--- |
| 2 | +title: WebGL Text and Annotations in R | Examples | Plotly |
| 3 | +name: WebGL Text and Annotations |
| 4 | +permalink: r/webgl-text-and-annotations/ |
| 5 | +description: How to add WebGL text labels and annotations to plots in R. |
| 6 | +layout: base |
| 7 | +thumbnail: thumbnail/webgl-text-and-annotations.jpg |
| 8 | +language: r |
| 9 | +page_type: example_index |
| 10 | +has_thumbnail: false |
| 11 | +display_as: style_opt |
| 12 | +order: 10 |
| 13 | +output: |
| 14 | + html_document: |
| 15 | + keep_md: true |
| 16 | +--- |
| 17 | + |
| 18 | +```{r, echo = FALSE, message=FALSE} |
| 19 | +knitr::opts_chunk$set(message = FALSE, warning=FALSE) |
| 20 | +Sys.setenv("plotly_username"="RPlotBot") |
| 21 | +Sys.setenv("plotly_api_key"="q0lz6r5efr") |
| 22 | +``` |
| 23 | +### New to Plotly? |
| 24 | + |
| 25 | +Plotly's R library is free and open source!<br> |
| 26 | +[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br> |
| 27 | +You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.<br> |
| 28 | +We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started! |
| 29 | + |
| 30 | +### Version Check |
| 31 | + |
| 32 | +Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br> |
| 33 | +Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version. |
| 34 | +```{r} |
| 35 | +library(plotly) |
| 36 | +packageVersion('plotly') |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | +### Heatmap with Annotations |
| 41 | + |
| 42 | +```{r, results = 'hide'} |
| 43 | +library(plotly) |
| 44 | +library(random) |
| 45 | +
|
| 46 | +n=250 |
| 47 | +t=12 |
| 48 | +x = c(rep(0:(n-1), times=t)) |
| 49 | +y = c(rep(0:(t-1), each=n)) |
| 50 | +z = randomNumbers(n=n*t, min=1, max=10, col=n) |
| 51 | +text = c(replicate(t*n, sample(c("A","T","G","C"), 1))) |
| 52 | +
|
| 53 | +steps = list() |
| 54 | +for (e in c(0:(n-30))){ |
| 55 | + step <- list( |
| 56 | + list( |
| 57 | + args = list('xaxis.range', c(-0.5 + e, 30.5 + e)), |
| 58 | + method = "relayout", |
| 59 | + label = e, |
| 60 | + value = e |
| 61 | + ) |
| 62 | + ) |
| 63 | + steps[e] <- step |
| 64 | +} |
| 65 | +
|
| 66 | +p <- plot_ly() %>% |
| 67 | + add_trace( |
| 68 | + type='heatmap', |
| 69 | + z = z |
| 70 | + ) %>% |
| 71 | + add_trace( |
| 72 | + mode = "text", |
| 73 | + text = text, |
| 74 | + type = "scattergl", |
| 75 | + textfont = list( |
| 76 | + size = 20 |
| 77 | + ), |
| 78 | + x = x, |
| 79 | + y = y |
| 80 | + ) %>% |
| 81 | + layout( |
| 82 | + xaxis = list( |
| 83 | + range = c(-0.5, 30.5), |
| 84 | + showline = F, |
| 85 | + zeroline = F, |
| 86 | + showgrid = F |
| 87 | + ), |
| 88 | + yaxis = list( |
| 89 | + showline = F, |
| 90 | + zeroline = F, |
| 91 | + showgrid = F |
| 92 | + ), |
| 93 | + sliders=list( |
| 94 | + list( |
| 95 | + active = 0, |
| 96 | + steps = steps |
| 97 | + ) |
| 98 | + ) |
| 99 | + ) |
| 100 | +
|
| 101 | +# Create a shareable link to your chart |
| 102 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 103 | +chart_link = api_create(p, filename="webgl-text-annotation") |
| 104 | +chart_link |
| 105 | +``` |
| 106 | + |
| 107 | +```{r, echo=FALSE} |
| 108 | +chart_link |
| 109 | +``` |
| 110 | + |
| 111 | +#Reference |
| 112 | + |
| 113 | +See [https://plot.ly/r/reference/#scattergl](https://plot.ly/r/reference/#scattergl) for more information and chart attribute options! |
0 commit comments