8000 image trace by Mahdis-z · Pull Request #30 · plotly/plotly.r-docs · GitHub
[go: up one dir, main page]

Skip to content

image trace #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
8000 Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ jobs:
name: Install container-level dependencies
command: |
sudo apt-get update -y
sudo apt-get install -y curl python3-pip python3-venv ssh-client ssh git
sudo apt-get install -y curl python3-pip python3-venv ssh-client ssh git
pip3 install virtualenv
- run:
name: Keyscan Github (HACK)
command: ssh-keyscan -H github.com >> ~/.ssh/known_hosts
- run:
name: install application-level dependencies
command: |
sudo apt-get install -y pandoc libudunits2-dev libgdal-dev libxt-dev libglu1-mesa-dev
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) '
sudo apt-get install -y pandoc libudunits2-dev libgdal-dev libxt-dev libglu1-mesa-dev libfftw3-dev
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) '
- save_cache:
key: cache3
paths:
Expand Down
83 changes: 83 additions & 0 deletions r/2020-02-25-imshow.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
name: Imshow
description: How to display image data in Python with R.
display_as: scientific
layout: base
language: r
order: 15
output:
html_document:
keep_md: true
permalink: r/imshow/
thumbnail: thumbnail/imshow.jpg
---

```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
```
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/).

### Display RGB Image Data with Image Trace

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.
```{r}
library(plotly)

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)
fig
```
### 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](https://www.rdocumentation.org/packages/EBImage/versions/4.14.2) to open an image from a URL.

```{r}
library(EBImage)

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)
fig
```
### Define the data range covered by the color range with zmin and zmax

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]`.
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]`.

```{r}
library(plotly)

img = readImage('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg')
# Stretch the contrast of the red channel only, resulting in a more red image
fig <- plot_ly(type="image", z=img*250,
zmin=c(10, 0, 0), zmax=c(200, 250, 250))
fig
```
### Set Ticks and Margins

```{r}
library(plotly)

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*250)
fig <- fig %>% layout(margin=list(l=10, r=10, b=0, t=0),
xaxis=list(showticklabels=FALSE, ticks=""),
yaxis=list(showticklabels=FALSE, ticks=""))
fig
```

### Combine image charts and other traces

```{r}
library(EBImage)
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*250)
fig <- fig %>% add_trace(
type='scatter', y=c(50, 60), x=c(40, 50),
marker=list(color='pink', size=10))
fig
```
### Reference
See [https://plot.ly/r/reference/#image](https://plot.ly/r/reference/#area) for more information and chart attribute options!
0