Display Image Data in R

How to display image data with R.

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.

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.

Display RGB Image Data with Image Trace

Note that Image trace only accepts multichannel images. For single images, use Heatmap. Image trace is different from the layout.Image class, which can be used for adding background images or logos to figures.


img_rgb = list(list(c(255, 0, 0),c(0, 255, 0),c(0, 0, 255)),
               list(c(0,255, 0),c(0, 0, 255),c(255, 0, 0)))
fig <- plot_ly(type="image", z=img_rgb)

Read image arrays from image files

In order to create a numerical array to be passed to Image trace, you can use a third-party library like EBImage to open an image from a URL.


img = readImage('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg')

fig <- plot_ly(type="image", z=img*255)