8000 Merge pull request #30 from plotly/imshow_r · plotly/plotly.r-docs@bc68256 · GitHub
[go: up one dir, main page]

Skip to content

Commit bc68256

Browse files
authored
Merge pull request #30 from plotly/imshow_r
image trace
2 parents 681bb0e + 8ea7c06 commit bc68256

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

.circleci/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ jobs:
1616
name: Install container-level dependencies
1717
command: |
1818
sudo apt-get update -y
19-
sudo apt-get install -y curl python3-pip python3-venv ssh-client ssh git
19+
sudo apt-get install -y curl python3-pip python3-venv ssh-client ssh git
2020
pip3 install virtualenv
2121
- run:
2222
name: Keyscan Github (HACK)
2323
command: ssh-keyscan -H github.com >> ~/.ssh/known_hosts
2424
- run:
2525
name: install application-level dependencies
2626
command: |
27-
sudo apt-get install -y pandoc libudunits2-dev libgdal-dev libxt-dev libglu1-mesa-dev
28-
sudo R -e 'install.packages(c("curl", "devtools", "mvtnorm")); devtools::install_github("hypertidy/anglr"); devtools::install_github("johannesbjork/LaCroixColoR"); devtools::install_github("ropensci/plotly"); devtools::install_deps(dependencies = TRUE) '
27+
sudo apt-get install -y pandoc libudunits2-dev libgdal-dev libxt-dev libglu1-mesa-dev libfftw3-dev
28+
sudo R -e 'install.packages(c("curl", "devtools", "mvtnorm")); devtools::install_github("hypertidy/anglr"); devtools::install_github("johannesbjork/LaCroixColoR"); devtools::install_github("ropensci/plotly"); install.packages("BiocManager"); BiocManager::install("EBImage"); devtools::install_deps(dependencies = TRUE) '
2929
- save_cache:
3030
key: cache3
3131
paths:

r/2020-02-25-imshow.Rmd

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)
0