|
| 1 | +--- |
| 2 | +name: Imshow |
| 3 | +description: How to display image data in Python with R. |
| 4 | +display_as: scientific |
| 5 | +layout: base |
| 6 | +language: r |
| 7 | +order: 15 |
| 8 | +output: |
| 9 | + html_document: |
| 10 | + keep_md: true |
| 11 | +permalink: r/imshow/ |
| 12 | +thumbnail: thumbnail/imshow.jpg |
| 13 | +--- |
| 14 | + |
| 15 | +```{r, echo = FALSE, message=FALSE} |
| 16 | +knitr::opts_chunk$set(message = FALSE, warning=FALSE) |
| 17 | +``` |
| 18 | +This tutorial shows how to display and explore image data. If you would like instead a logo or static image, use `layout.Image` as explained [here](https://plot.ly/r/logos/). |
| 19 | + |
| 20 | +### Display RGB Image Data with Image Trace |
| 21 | + |
| 22 | +Note that `Image` trace only accepts multichannel images. For single images, use [`Heatmap`](https://plot.ly/r/heatmaps/). `Image` trace is different from the `layout.Image` class, which can be used for adding background images or logos to figures. |
| 23 | +```{r} |
| 24 | +library(plotly) |
| 25 | +
|
| 26 | +img_rgb = list(list(c(255, 0, 0),c(0, 255, 0),c(0, 0, 255)), |
| 27 | + list(c(0,255, 0),c(0, 0, 255),c(255, 0, 0))) |
| 28 | +fig <- plot_ly(type="image", z=img_rgb) |
| 29 | +fig |
| 30 | +``` |
| 31 | +### Read image arrays from image files |
| 32 | + |
| 33 | +In order to create a numerical array to be passed to `Image` trace, you can use a third-party library like [EBImage](https://www.rdocumentation.org/packages/EBImage/versions/4.14.2) to open an image from a URL. |
| 34 | + |
| 35 | +```{r} |
| 36 | +library(EBImage) |
| 37 | +
|
| 38 | +img = readImage('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg') |
| 39 | +
|
| 40 | +fig <- plot_ly(type="image", z=img*255) |
| 41 | +fig |
| 42 | +``` |
| 43 | +### Define the data range covered by the color range with zmin and zmax |
| 44 | + |
| 45 | +The data range and color range are mapped together using the parameters `zmin` and `zmax`, which correspond respectively to the data values mapped to black `[0, 0, 0]` and white `[255, 255, 255]`. |
| 46 | +The default value of `zmin` and [zmax](https://plot.ly/r/reference/#image-zmax) depends on the `colormodal` value. In this example `colormodel is "rgb"`(by default), so the default value of `zmin is [0, 0, 0]` and `zmax is [250, 250, 250]`. |
| 47 | + |
| 48 | +```{r} |
| 49 | +library(plotly) |
| 50 | +
|
| 51 | +img = readImage('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg') |
| 52 | +# Stretch the contrast of the red channel only, resulting in a more red image |
| 53 | +fig <- plot_ly(type="image", z=img*250, |
| 54 | + zmin=c(10, 0, 0), zmax=c(200, 250, 250)) |
| 55 | +fig |
| 56 | +``` |
| 57 | +### Set Ticks and Margins |
| 58 | + |
| 59 | +```{r} |
| 60 | +library(plotly) |
| 61 | +
|
| 62 | +img = readImage('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg') |
| 63 | +fig <- plot_ly(type="image", z=img*250) |
| 64 | +fig <- fig %>% layout(margin=list(l=10, r=10, b=0, t=0), |
| 65 | + xaxis=list(showticklabels=FALSE, ticks=""), |
| 66 | + yaxis=list(showticklabels=FALSE, ticks="")) |
| 67 | +fig |
| 68 | +``` |
| 69 | + |
| 70 | +### Combine image charts and other traces |
| 71 | + |
| 72 | +```{r} |
| 73 | +library(EBImage) |
| 74 | +img = readImage('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg') |
| 75 | +
|
| 76 | +fig <- plot_ly(type="image", z=img*250) |
| 77 | +fig <- fig %>% add_trace( |
| 78 | + type='scatter', y=c(50, 60), x=c(40, 50), |
| 79 | + marker=list(color='pink', size=10)) |
| 80 | +fig |
| 81 | +``` |
| 82 | +### Reference |
| 83 | +See [https://plot.ly/r/reference/#image](https://plot.ly/r/reference/#area) for more information and chart attribute options! |
0 commit comments