diff --git a/.circleci/config.yml b/.circleci/config.yml index 772b30cd..2f7c3d75 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -16,7 +16,7 @@ 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) @@ -24,8 +24,8 @@ jobs: - 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: diff --git a/r/2020-02-25-imshow.Rmd b/r/2020-02-25-imshow.Rmd new file mode 100644 index 00000000..10a09635 --- /dev/null +++ b/r/2020-02-25-imshow.Rmd @@ -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!