From 36f7b564b25cd844be528f8d3b3a19cb29494b61 Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Mon, 12 Aug 2019 15:40:46 -0400 Subject: [PATCH 1/2] new version --- _posts/ggplot2/2016-11-29-geom_tile.Rmd | 128 ------------------------ _posts/ggplot2/2016-11-29-geom_tile.md | 126 ----------------------- _posts/ggplot2/2019-08-12-geom_tile.Rmd | 115 +++++++++++++++++++++ _posts/ggplot2/2019-08-12-geom_tile.md | 113 +++++++++++++++++++++ 4 files changed, 228 insertions(+), 254 deletions(-) delete mode 100644 _posts/ggplot2/2016-11-29-geom_tile.Rmd delete mode 100644 _posts/ggplot2/2016-11-29-geom_tile.md create mode 100644 _posts/ggplot2/2019-08-12-geom_tile.Rmd create mode 100644 _posts/ggplot2/2019-08-12-geom_tile.md diff --git a/_posts/ggplot2/2016-11-29-geom_tile.Rmd b/_posts/ggplot2/2016-11-29-geom_tile.Rmd deleted file mode 100644 index cf82ede17a62..000000000000 --- a/_posts/ggplot2/2016-11-29-geom_tile.Rmd +++ /dev/null @@ -1,128 +0,0 @@ ---- -title: geom_tile | Examples | Plotly -name: geom_tile -permalink: ggplot2/geom_tile/ -description: Use geom_tile to create heatmaps in R using ggplot2 -layout: base -thumbnail: thumbnail/heatmap.jpg -language: ggplot2 -page_type: example_index -has_thumbnail: true -display_as: basic -order: 8 -output: - html_document: - keep_md: true ---- - -```{r, echo = FALSE, message=FALSE} -knitr::opts_chunk$set(message = FALSE, warning=FALSE) -Sys.setenv("plotly_username"="RPlotBot") -Sys.setenv("plotly_api_key"="q0lz6r5efr") -``` - -### New to Plotly? - -Plotly's R library is free and open source!
-[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).
-You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.
-We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started! - -### Version Check - -Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!
-Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version. - -```{r} -library(plotly) -packageVersion('plotly') -``` - -### Matrix - -```{r, results='hide'} -library(plotly) -library(reshape2) - -p <- volcano %>% - melt() %>% - ggplot(aes(Var1, Var2, fill = value)) + geom_tile() + - labs(x = "West to East", - y="North to South", - title = "Visualizing the Topography of Maunga Whau") - -p <- ggplotly(p) - -# Create a shareable link to your chart -# Set up API credentials: https://plot.ly/r/getting-started -chart_link = plotly_POST(p, filename="geom_tile/matrix") -chart_link -``` - -``` {r, echo=FALSE} -chart_link -``` -Inspired by ggplot2 documentation - -### Hierarchial Clustering - -```{r, results='hide'} -library(plotly) -library(reshape2) - -# Do hierarchical clustering - -clust <- volcano %>% - dist() %>% - hclust() - -# Get order -ord <- clust$order - -# Re-arrange based on order -df <- volcano[ord,] - -# Plot -p <- df %>% - melt() %>% - ggplot(aes(Var1, Var2, fill = value)) + geom_tile() - -p <- ggplotly(p) - -# Create a shareable link to your chart -# Set up API credentials: https://plot.ly/r/getting-started -chart_link = plotly_POST(p, filename="geom_tile/clustering") -chart_link -``` - -``` {r, echo=FALSE} -chart_link -``` -Inspired by Stack Overflow - -### Modified Colorscale - -```{r, results='hide'} -library(plotly) -library(reshape2) - -p <- volcano %>% - melt() %>% - ggplot(aes(Var1, Var2, fill = value)) + geom_tile() + - scale_fill_gradient(low="#ffe6cc", high="#ff8400") + - labs(x = "West to East", - y="North to South", - title = "Visualizing the Topography of Maunga Whau") - -p <- ggplotly(p) - -# Create a shareable link to your chart -# Set up API credentials: https://plot.ly/r/getting-started -chart_link = plotly_POST(p, filename="geom_tile/colorscale") -chart_link -``` - -``` {r, echo=FALSE} -chart_link -``` -Inspired by ggplot2 documentation diff --git a/_posts/ggplot2/2016-11-29-geom_tile.md b/_posts/ggplot2/2016-11-29-geom_tile.md deleted file mode 100644 index dea0a2ee34e1..000000000000 --- a/_posts/ggplot2/2016-11-29-geom_tile.md +++ /dev/null @@ -1,126 +0,0 @@ ---- -title: geom_tile | Examples | Plotly -name: geom_tile -permalink: ggplot2/geom_tile/ -description: Use geom_tile to create heatmaps in R using ggplot2 -layout: base -thumbnail: thumbnail/heatmap.jpg -language: ggplot2 -page_type: example_index -has_thumbnail: true -display_as: basic -order: 8 -output: - html_document: - keep_md: true ---- - - - -### New to Plotly? - -Plotly's R library is free and open source!
-[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).
-You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.
-We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started! - -### Version Check - -Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!
-Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version. - - -```r -library(plotly) -packageVersion('plotly') -``` - -``` -## [1] '4.9.0' -``` - -### Matrix - - -```r -library(plotly) -library(reshape2) - -p <- volcano %>% - melt() %>% - ggplot(aes(Var1, Var2, fill = value)) + geom_tile() + - labs(x = "West to East", - y="North to South", - title = "Visualizing the Topography of Maunga Whau") - -p <- ggplotly(p) - -# Create a shareable link to your chart -# Set up API credentials: https://plot.ly/r/getting-started -chart_link = plotly_POST(p, filename="geom_tile/matrix") -chart_link -``` - - -Inspired by ggplot2 documentation - -### Hierarchial Clustering - - -```r -library(plotly) -library(reshape2) - -# Do hierarchical clustering - -clust <- volcano %>% - dist() %>% - hclust() - -# Get order -ord <- clust$order - -# Re-arrange based on order -df <- volcano[ord,] - -# Plot -p <- df %>% - melt() %>% - ggplot(aes(Var1, Var2, fill = value)) + geom_tile() - -p <- ggplotly(p) - -# Create a shareable link to your chart -# Set up API credentials: https://plot.ly/r/getting-started -chart_link = plotly_POST(p, filename="geom_tile/clustering") -chart_link -``` - - -Inspired by Stack Overflow - -### Modified Colorscale - - -```r -library(plotly) -library(reshape2) - -p <- volcano %>% - melt() %>% - ggplot(aes(Var1, Var2, fill = value)) + geom_tile() + - scale_fill_gradient(low="#ffe6cc", high="#ff8400") + - labs(x = "West to East", - y="North to South", - title = "Visualizing the Topography of Maunga Whau") - -p <- ggplotly(p) - -# Create a shareable link to your chart -# Set up API credentials: https://plot.ly/r/getting-started -chart_link = plotly_POST(p, filename="geom_tile/colorscale") -chart_link -``` - - -Inspired by ggplot2 documentation diff --git a/_posts/ggplot2/2019-08-12-geom_tile.Rmd b/_posts/ggplot2/2019-08-12-geom_tile.Rmd new file mode 100644 index 000000000000..3c2779899bf6 --- /dev/null +++ b/_posts/ggplot2/2019-08-12-geom_tile.Rmd @@ -0,0 +1,115 @@ +--- +title: geom_tile | Examples | Plotly +name: geom_tile +permalink: ggplot2/geom_tile/ +description: How to make a 2-dimensional heatmap in ggplot2 using geom_tile. +layout: base +thumbnail: thumbnail/geom_tile.jpg +language: ggplot2 +page_type: example_index +has_thumbnail: true +display_as: basic +order: 8 +output: + html_document: + keep_md: true +--- + +```{r, echo = FALSE, message=FALSE} +knitr::opts_chunk$set(message = FALSE, warning=FALSE) +Sys.setenv("plotly_username"="RPlotBot") +Sys.setenv("plotly_api_key"="q0lz6r5efr") +``` + +### New to Plotly? + +Plotly's R library is free and open source!
+[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).
+You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.
+We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started! + +### Version Check + +Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!
+Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version. + +```{r} +library(plotly) +packageVersion('plotly') +``` + +### Basic geom\_tile graph +This graph, compiled by [Jeff Zimmerman](https://docs.google.com/spreadsheets/d/1HI-dikWN64clxSGJu2QV8C64R9Bkzt8K-jFaeHj4X7k/edit#gid=0), shows how often hitters swing and miss at fastballs, based on their velocity and spin rate. Colour schemes are from ColorBrewer; a complete list of palettes is available [here](https://ggplot2.tidyverse.org/reference/scale_brewer.html). + +```{r, results='hide'} +library(plotly) +spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv", + stringsAsFactors = FALSE) + +p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + + geom_tile(aes(fill = swing_miss)) + + scale_fill_distiller(palette = "YlGnBu") + + labs(title = "Likelihood of swinging and missing on a fastball", + y = "spin rate (rpm)") +ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_tile/distilled-colour") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### Adjusting appearance +The *direction* option sets which side of the colour scheme maps onto the low values and which side maps onto the high; it defaults to -1 but could be adjusted to 1. + +Also [adjusted the theme](https://ggplot2.tidyverse.org/reference/ggtheme.html). + +```{r, results='hide'} +library(plotly) + +p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + + geom_tile(aes(fill = swing_miss)) + + scale_fill_distiller(palette = "YlGnBu", direction = 1) + + theme_light() + + labs(title = "Likelihood of swinging and missing on a fastball", + y = "spin rate (rpm)") +ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_tile/customize-theme") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### geom\_tile with viridis colour scheme +[Viridis colour schemes](https://ggplot2.tidyverse.org/reference/scale_brewer.html) are uniform in both colour and black-and-white, as well as for those with colour-blindness. There are five colour schemes: "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"), "viridis" (or "D", the default option) and "cividis" (or "E"). + +```{r, results='hide'} +library(plotly) + +p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + + geom_tile(aes(fill = swing_miss)) + + scale_fill_viridis_c(option = "B", direction = -1) + + labs(title = "Likelihood of swinging and missing on a fastball", + y = "spin rate (rpm)") + + theme_light() +ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_tile/viridis") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + diff --git a/_posts/ggplot2/2019-08-12-geom_tile.md b/_posts/ggplot2/2019-08-12-geom_tile.md new file mode 100644 index 000000000000..9eb48f76e328 --- /dev/null +++ b/_posts/ggplot2/2019-08-12-geom_tile.md @@ -0,0 +1,113 @@ +--- +title: geom_tile | Examples | Plotly +name: geom_tile +permalink: ggplot2/geom_tile/ +description: How to make a 2-dimensional heatmap in ggplot2 using geom_tile. +layout: base +thumbnail: thumbnail/geom_tile.jpg +language: ggplot2 +page_type: example_index +has_thumbnail: true +display_as: basic +order: 8 +output: + html_document: + keep_md: true +--- + + + +### New to Plotly? + +Plotly's R library is free and open source!
+[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).
+You can set up Plotly to work in [online](https://plot.ly/r/getting-started/#hosting-graphs-in-your-online-plotly-account) or [offline](https://plot.ly/r/offline/) mode.
+We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started! + +### Version Check + +Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!
+Check out [this post](http://moderndata.plot.ly/upgrading-to-plotly-4-0-and-above/) for more information on breaking changes and new features available in this version. + + +```r +library(plotly) +packageVersion('plotly') +``` + +``` +## [1] '4.8.0.9000' +``` + +### Basic geom\_tile graph +This graph, compiled by [Jeff Zimmerman](https://docs.google.com/spreadsheets/d/1HI-dikWN64clxSGJu2QV8C64R9Bkzt8K-jFaeHj4X7k/edit#gid=0), shows how often hitters swing and miss at fastballs, based on their velocity and spin rate. Colour schemes are from ColorBrewer; a complete list of palettes is available [here](https://ggplot2.tidyverse.org/reference/scale_brewer.html). + + +```r +library(plotly) +spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv", + stringsAsFactors = FALSE) + +p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + + geom_tile(aes(fill = swing_miss)) + + scale_fill_distiller(palette = "YlGnBu") + + labs(title = "Likelihood of swinging and missing on a fastball", + y = "spin rate (rpm)") +ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_tile/distilled-colour") +chart_link +``` + + + +### Adjusting appearance +The *direction* option sets which side of the colour scheme maps onto the low values and which side maps onto the high; it defaults to -1 but could be adjusted to 1. + +Also [adjusted the theme](https://ggplot2.tidyverse.org/reference/ggtheme.html). + + +```r +library(plotly) + +p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + + geom_tile(aes(fill = swing_miss)) + + scale_fill_distiller(palette = "YlGnBu", direction = 1) + + theme_light() + + labs(title = "Likelihood of swinging and missing on a fastball", + y = "spin rate (rpm)") +ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_tile/customize-theme") +chart_link +``` + + + +### geom\_tile with viridis colour scheme +[Viridis colour schemes](https://ggplot2.tidyverse.org/reference/scale_brewer.html) are uniform in both colour and black-and-white, as well as for those with colour-blindness. There are five colour schemes: "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"), "viridis" (or "D", the default option) and "cividis" (or "E"). + + +```r +library(plotly) + +p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + + geom_tile(aes(fill = swing_miss)) + + scale_fill_viridis_c(option = "B", direction = -1) + + labs(title = "Likelihood of swinging and missing on a fastball", + y = "spin rate (rpm)") + + theme_light() +ggplotly(p) + +# Create a shareable link to your chart +# Set up API credentials: https://plot.ly/r/getting-started +chart_link = api_create(p, filename="geom_tile/viridis") +chart_link +``` + + + From b4f9f758fb4074cf485f7607b97f501515aadd7d Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Mon, 12 Aug 2019 18:11:39 -0400 Subject: [PATCH 2/2] edited --- _posts/ggplot2/2019-08-12-geom_tile.Rmd | 4 ++++ _posts/ggplot2/2019-08-12-geom_tile.md | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/_posts/ggplot2/2019-08-12-geom_tile.Rmd b/_posts/ggplot2/2019-08-12-geom_tile.Rmd index 3c2779899bf6..c23728ab86c6 100644 --- a/_posts/ggplot2/2019-08-12-geom_tile.Rmd +++ b/_posts/ggplot2/2019-08-12-geom_tile.Rmd @@ -70,6 +70,8 @@ Also [adjusted the theme](https://ggplot2.tidyverse.org/reference/ggtheme.html). ```{r, results='hide'} library(plotly) +spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv", + stringsAsFactors = FALSE) p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + geom_tile(aes(fill = swing_miss)) + @@ -94,6 +96,8 @@ chart_link ```{r, results='hide'} library(plotly) +spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv", + stringsAsFactors = FALSE) p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + geom_tile(aes(fill = swing_miss)) + diff --git a/_posts/ggplot2/2019-08-12-geom_tile.md b/_posts/ggplot2/2019-08-12-geom_tile.md index 9eb48f76e328..f66777146964 100644 --- a/_posts/ggplot2/2019-08-12-geom_tile.md +++ b/_posts/ggplot2/2019-08-12-geom_tile.md @@ -71,6 +71,8 @@ Also [adjusted the theme](https://ggplot2.tidyverse.org/reference/ggtheme.html). ```r library(plotly) +spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv", + stringsAsFactors = FALSE) p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + geom_tile(aes(fill = swing_miss)) + @@ -94,6 +96,8 @@ chart_link ```r library(plotly) +spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv", + stringsAsFactors = FALSE) p <- ggplot(spinrates, aes(x=velocity, y=spinrate)) + geom_tile(aes(fill = swing_miss)) +