From e4ef53afe75294d14d7da37c3f747575d6ac5986 Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Fri, 9 Aug 2019 15:23:23 -0400 Subject: [PATCH 1/2] new geom --- _posts/ggplot2/2019-08-09-geom_contour.Rmd | 109 +++++++++++++++++++++ _posts/ggplot2/2019-08-09-geom_contour.md | 107 ++++++++++++++++++++ 2 files changed, 216 insertions(+) create mode 100644 _posts/ggplot2/2019-08-09-geom_contour.Rmd create mode 100644 _posts/ggplot2/2019-08-09-geom_contour.md diff --git a/_posts/ggplot2/2019-08-09-geom_contour.Rmd b/_posts/ggplot2/2019-08-09-geom_contour.Rmd new file mode 100644 index 000000000000..72a5e907e847 --- /dev/null +++ b/_posts/ggplot2/2019-08-09-geom_contour.Rmd @@ -0,0 +1,109 @@ +--- +title: geom_contour | Examples | Plotly +name: geom_contour +permalink: ggplot2/geom_contour/ +description: How to make a contour in ggplot2 using geom_contour. +layout: base +thumbnail: thumbnail/geom_contour.jpg +language: ggplot2 +page_type: example_index +has_thumbnail: true +display_as: basic +order: 3 +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\_contour plot +geom\_contour produces a similar output to geom\_density\_2d, except it uses a third variable for the values rather than frequency. The volcano dataset comes pre-loaded on R. + +```{r, results='hide'} +library(plotly) +library(reshape2) +df <- melt(volcano) + +p <- ggplot(df, aes(Var1, Var2, z= value)) + + geom_contour() + + scale_fill_distiller(palette = "Spectral", direction = -1) +p <- 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_contour/basic-plot") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### Coloured Plot +[See here](https://ggplot2.tidyverse.org/reference/scale_brewer.html) for a list of colour palettes that come with the brewer (discrete) and distiller (continuous) packages. + +```{r, results='hide'} +library(plotly) +library(reshape2) +df <- melt(volcano) + +p <- ggplot(df, aes(Var1, Var2, z= value, colour=stat(level))) + + geom_contour() + + scale_colour_distiller(palette = "YlGn", direction = 1) +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_contour/coloured-plot") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### Filled Plot +It's possible to colour in each of the layers, by changing geom\_contour to stat\_contour as below. (As the edges of the graph indicate, filled contour plots only work when each layer is an enclosed shape rather than an open line.) + +```{r, results='hide'} +library(plotly) +library(reshape2) +df <- melt(volcano) + +p <- ggplot(df, aes(Var1, Var2, z= value)) + + stat_contour(geom="polygon",aes(fill=stat(level))) + + scale_fill_distiller(palette = "Spectral", direction = -1) +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_contour/filled-plot") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + diff --git a/_posts/ggplot2/2019-08-09-geom_contour.md b/_posts/ggplot2/2019-08-09-geom_contour.md new file mode 100644 index 000000000000..9542df8f65f4 --- /dev/null +++ b/_posts/ggplot2/2019-08-09-geom_contour.md @@ -0,0 +1,107 @@ +--- +title: geom_contour | Examples | Plotly +name: geom_contour +permalink: ggplot2/geom_contour/ +description: How to make a contour in ggplot2 using geom_contour. +layout: base +thumbnail: thumbnail/geom_contour.jpg +language: ggplot2 +page_type: example_index +has_thumbnail: true +display_as: basic +order: 3 +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\_contour plot +geom\_contour produces a similar output to geom\_density\_2d, except it uses a third variable for the values rather than frequency. The volcano dataset comes pre-loaded on R. + + +```r +library(plotly) +library(reshape2) +df <- melt(volcano) + +p <- ggplot(df, aes(Var1, Var2, z= value)) + + geom_contour() + + scale_fill_distiller(palette = "Spectral", direction = -1) +p <- 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_contour/basic-plot") +chart_link +``` + + + +### Coloured Plot +[See here](https://ggplot2.tidyverse.org/reference/scale_brewer.html) for a list of colour palettes that come with the brewer (discrete) and distiller (continuous) packages. + + +```r +library(plotly) +library(reshape2) +df <- melt(volcano) + +p <- ggplot(df, aes(Var1, Var2, z= value, colour=stat(level))) + + geom_contour() + + scale_colour_distiller(palette = "YlGn", direction = 1) +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_contour/coloured-plot") +chart_link +``` + + + +### Filled Plot +It's possible to colour in each of the layers, by changing geom\_contour to stat\_contour as below. (As the edges of the graph indicate, filled contour plots only work when each layer is an enclosed shape rather than an open line.) + + +```r +library(plotly) +library(reshape2) +df <- melt(volcano) + +p <- ggplot(df, aes(Var1, Var2, z= value)) + + stat_contour(geom="polygon",aes(fill=stat(level))) + + scale_fill_distiller(palette = "Spectral", direction = -1) +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_contour/filled-plot") +chart_link +``` + + + From 276434b0da4b3c4babcddb0c07904288d75d76dd Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Mon, 12 Aug 2019 17:20:29 -0400 Subject: [PATCH 2/2] edited text --- _posts/ggplot2/2019-08-09-geom_contour.Rmd | 2 +- _posts/ggplot2/2019-08-09-geom_contour.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/_posts/ggplot2/2019-08-09-geom_contour.Rmd b/_posts/ggplot2/2019-08-09-geom_contour.Rmd index 72a5e907e847..7789dae29d51 100644 --- a/_posts/ggplot2/2019-08-09-geom_contour.Rmd +++ b/_posts/ggplot2/2019-08-09-geom_contour.Rmd @@ -85,7 +85,7 @@ chart_link ``` ### Filled Plot -It's possible to colour in each of the layers, by changing geom\_contour to stat\_contour as below. (As the edges of the graph indicate, filled contour plots only work when each layer is an enclosed shape rather than an open line.) +It's possible to colour in each of the layers, by changing geom\_contour to stat\_contour as below. As the edges of the graph indicate, filled contour plots only work when each layer is an enclosed shape rather than an open line; a geom more suited to this functionality would be geom\_tile or geom\_raster. ```{r, results='hide'} library(plotly) diff --git a/_posts/ggplot2/2019-08-09-geom_contour.md b/_posts/ggplot2/2019-08-09-geom_contour.md index 9542df8f65f4..5fce03a95269 100644 --- a/_posts/ggplot2/2019-08-09-geom_contour.md +++ b/_posts/ggplot2/2019-08-09-geom_contour.md @@ -84,7 +84,7 @@ chart_link ### Filled Plot -It's possible to colour in each of the layers, by changing geom\_contour to stat\_contour as below. (As the edges of the graph indicate, filled contour plots only work when each layer is an enclosed shape rather than an open line.) +It's possible to colour in each of the layers, by changing geom\_contour to stat\_contour as below. As the edges of the graph indicate, filled contour plots only work when each layer is an enclosed shape rather than an open line; a geom more suited to this functionality would be geom\_tile or geom\_raster. ```r