From 72bcc6f89876622dc629713c3abea8a9789fa868 Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Wed, 7 Aug 2019 13:36:45 -0400 Subject: [PATCH 1/3] new geom! --- _posts/ggplot2/2019-08-06-geom_density_2d.Rmd | 153 ++++++++++++++++++ _posts/ggplot2/2019-08-06-geom_density_2d.md | 149 +++++++++++++++++ 2 files changed, 302 insertions(+) create mode 100644 _posts/ggplot2/2019-08-06-geom_density_2d.Rmd create mode 100644 _posts/ggplot2/2019-08-06-geom_density_2d.md diff --git a/_posts/ggplot2/2019-08-06-geom_density_2d.Rmd b/_posts/ggplot2/2019-08-06-geom_density_2d.Rmd new file mode 100644 index 000000000000..6431d3ead619 --- /dev/null +++ b/_posts/ggplot2/2019-08-06-geom_density_2d.Rmd @@ -0,0 +1,153 @@ +--- +title: geom_density_2d | Examples | Plotly +name: geom_density_2d +permalink: ggplot2/geom_density_2d/ +description: How to make a density map using geom_density_2d. +layout: base +thumbnail: thumbnail/geom_density_2d.jpg +language: ggplot2 +page_type: example_index +has_thumbnail: true +display_as: statistical +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 2D Graph +Source: [Brett Carpenter from Data.World](https://data.world/brettcarpenter/craft-beer-data) + +```{r, results='hide'} +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) + +p <- ggplot(beers, aes(x=abv, y=ibu)) + + geom_density_2d() + + labs(y = "bitterness (IBU)", + x = "alcohol volume (ABV)", + title = "Craft beers from American breweries") +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_density_2d/basic-graph") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### Filled +Since each of the lines (in the above graph) shows a different "level", setting "fill = stat(level)" allows for a filled graph. + +```{r, results='hide'} +p <- ggplot(beers, aes(x=abv, y=ibu)) + + stat_density_2d(aes(fill = stat(level)), geom="polygon") + + labs(y = "bitterness (IBU)", + x = "alcohol volume (ABV)", + title = "Craft beers from American breweries") +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_density_2d/filled") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### Preset Colourscale +["Viridis" colourscales](https://ggplot2.tidyverse.org/reference/scale_viridis.html) are designed to still be perceptible in black-and-white, as well as for those with colourblindness. It comes with five colourscales, selected using the option= parameter: "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"), "viridis" (or "D", the default), and "cividis" (or "E"). +```{r, results='hide'} +p <- ggplot(beers, aes(x=abv, y=ibu)) + + stat_density_2d(aes(fill = stat(level)), geom="polygon") + + scale_fill_viridis_c(option = "plasma") + + theme(legend.position = "magma") + + labs(y = "bitterness (IBU)", + x = "alcohol volume (ABV)", + title = "Craft beers from American breweries") +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_density_2d/preset-colours") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### Customized Colourscale +You can also set your own colour gradients by defining a high and low point. +```{r, results='hide'} +p <- ggplot(beers, aes(x=abv, y=ibu)) + + stat_density_2d(aes(fill = stat(level)), geom="polygon") + + scale_fill_gradient(low = "lightskyblue1", high = "darkred") + + theme(legend.position = "none") + + labs(y = "bitterness (IBU)", + x = "alcohol volume (ABV)", + title = "Craft beers from American breweries") +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_density_2d/customized-colours") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + +### Overlaid Points +I use variable "style2" to filter out the six most common beer styles. This way, we can see that the cluster of beers in the top right (i.e. more bitter and higher alcohol content) are IPAs - perhaps unsurprisingly. + +```{r, results='hide'} +library(dplyr) + +p <- ggplot(beers, aes(x=abv, y=ibu)) + + geom_density_2d(alpha=0.5) + + geom_point(data=filter(beers, !is.na(style2)), aes(colour=style2, text = label), alpha=0.3) + + labs(y = "bitterness (IBU)", + x = "alcohol volume (ABV)", + title = "Craft beers from American breweries", + colour = "Beer types") +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_density_2d/overlaid-points") +chart_link +``` + +```{r echo=FALSE} +chart_link +``` + diff --git a/_posts/ggplot2/2019-08-06-geom_density_2d.md b/_posts/ggplot2/2019-08-06-geom_density_2d.md new file mode 100644 index 000000000000..cb4b58b4131d --- /dev/null +++ b/_posts/ggplot2/2019-08-06-geom_density_2d.md @@ -0,0 +1,149 @@ +--- +title: geom_density_2d | Examples | Plotly +name: geom_density_2d +permalink: ggplot2/geom_density_2d/ +description: How to make a density map using geom_density_2d. +layout: base +thumbnail: thumbnail/geom_density_2d.jpg +language: ggplot2 +page_type: example_index +has_thumbnail: true +display_as: statistical +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 2D Graph +Source: [Brett Carpenter from Data.World](https://data.world/brettcarpenter/craft-beer-data) + + +```r +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) + +p <- ggplot(beers, aes(x=abv, y=ibu)) + + geom_density_2d() + + labs(y = "bitterness (IBU)", + x = "alcohol volume (ABV)", + title = "Craft beers from American breweries") +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_density_2d/basic-graph") +chart_link +``` + + + +### Filled +Since each of the lines (in the above graph) shows a different "level", setting "fill = stat(level)" allows for a filled graph. + + +```r +p <- ggplot(beers, aes(x=abv, y=ibu)) + + stat_density_2d(aes(fill = stat(level)), geom="polygon") + + labs(y = "bitterness (IBU)", + x = "alcohol volume (ABV)", + title = "Craft beers from American breweries") +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_density_2d/filled") +chart_link +``` + + + +### Preset Colourscale +["Viridis" colourscales](https://ggplot2.tidyverse.org/reference/scale_viridis.html) are designed to still be perceptible in black-and-white, as well as for those with colourblindness. It comes with five colourscales, selected using the option= parameter: "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"), "viridis" (or "D", the default), and "cividis" (or "E"). + +```r +p <- ggplot(beers, aes(x=abv, y=ibu)) + + stat_density_2d(aes(fill = stat(level)), geom="polygon") + + scale_fill_viridis_c(option = "plasma") + + theme(legend.position = "magma") + + labs(y = "bitterness (IBU)", + x = "alcohol volume (ABV)", + title = "Craft beers from American breweries") +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_density_2d/preset-colours") +chart_link +``` + + + +### Customized Colourscale +You can also set your own colour gradients by defining a high and low point. + +```r +p <- ggplot(beers, aes(x=abv, y=ibu)) + + stat_density_2d(aes(fill = stat(level)), geom="polygon") + + scale_fill_gradient(low = "lightskyblue1", high = "darkred") + + theme(legend.position = "none") + + labs(y = "bitterness (IBU)", + x = "alcohol volume (ABV)", + title = "Craft beers from American breweries") +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_density_2d/customized-colours") +chart_link +``` + + + +### Overlaid Points +I use variable "style2" to filter out the six most common beer styles. This way, we can see that the cluster of beers in the top right (i.e. more bitter and higher alcohol content) are IPAs - perhaps unsurprisingly. + + +```r +library(dplyr) + +p <- ggplot(beers, aes(x=abv, y=ibu)) + + geom_density_2d(alpha=0.5) + + geom_point(data=filter(beers, !is.na(style2)), aes(colour=style2, text = label), alpha=0.3) + + labs(y = "bitterness (IBU)", + x = "alcohol volume (ABV)", + title = "Craft beers from American breweries", + colour = "Beer types") +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_density_2d/overlaid-points") +chart_link +``` + + + From b82599991fef4ac2195933a7bb373f210a0a309c Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Mon, 12 Aug 2019 18:00:02 -0400 Subject: [PATCH 2/3] fix --- _posts/ggplot2/2019-08-06-geom_density_2d.Rmd | 13 +++++++++++++ _posts/ggplot2/2019-08-06-geom_density_2d.md | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/_posts/ggplot2/2019-08-06-geom_density_2d.Rmd b/_posts/ggplot2/2019-08-06-geom_density_2d.Rmd index 6431d3ead619..bfa2ebf51d44 100644 --- a/_posts/ggplot2/2019-08-06-geom_density_2d.Rmd +++ b/_posts/ggplot2/2019-08-06-geom_density_2d.Rmd @@ -42,6 +42,7 @@ packageVersion('plotly') Source: [Brett Carpenter from Data.World](https://data.world/brettcarpenter/craft-beer-data) ```{r, results='hide'} +library(plotly) beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + @@ -65,6 +66,9 @@ chart_link Since each of the lines (in the above graph) shows a different "level", setting "fill = stat(level)" allows for a filled graph. ```{r, results='hide'} +library(plotly) +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) + p <- ggplot(beers, aes(x=abv, y=ibu)) + stat_density_2d(aes(fill = stat(level)), geom="polygon") + labs(y = "bitterness (IBU)", @@ -84,7 +88,11 @@ chart_link ### Preset Colourscale ["Viridis" colourscales](https://ggplot2.tidyverse.org/reference/scale_viridis.html) are designed to still be perceptible in black-and-white, as well as for those with colourblindness. It comes with five colourscales, selected using the option= parameter: "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"), "viridis" (or "D", the default), and "cividis" (or "E"). + ```{r, results='hide'} +library(plotly) +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) + p <- ggplot(beers, aes(x=abv, y=ibu)) + stat_density_2d(aes(fill = stat(level)), geom="polygon") + scale_fill_viridis_c(option = "plasma") + @@ -107,6 +115,9 @@ chart_link ### Customized Colourscale You can also set your own colour gradients by defining a high and low point. ```{r, results='hide'} +library(plotly) +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) + p <- ggplot(beers, aes(x=abv, y=ibu)) + stat_density_2d(aes(fill = stat(level)), geom="polygon") + scale_fill_gradient(low = "lightskyblue1", high = "darkred") + @@ -130,7 +141,9 @@ chart_link I use variable "style2" to filter out the six most common beer styles. This way, we can see that the cluster of beers in the top right (i.e. more bitter and higher alcohol content) are IPAs - perhaps unsurprisingly. ```{r, results='hide'} +library(plotly) library(dplyr) +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + geom_density_2d(alpha=0.5) + diff --git a/_posts/ggplot2/2019-08-06-geom_density_2d.md b/_posts/ggplot2/2019-08-06-geom_density_2d.md index cb4b58b4131d..09c1d479617b 100644 --- a/_posts/ggplot2/2019-08-06-geom_density_2d.md +++ b/_posts/ggplot2/2019-08-06-geom_density_2d.md @@ -44,6 +44,7 @@ Source: [Brett Carpenter from Data.World](https://data.world/brettcarpenter/craf ```r +library(plotly) beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + @@ -66,6 +67,9 @@ Since each of the lines (in the above graph) shows a different "level", setting ```r +library(plotly) +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) + p <- ggplot(beers, aes(x=abv, y=ibu)) + stat_density_2d(aes(fill = stat(level)), geom="polygon") + labs(y = "bitterness (IBU)", @@ -85,6 +89,9 @@ chart_link ["Viridis" colourscales](https://ggplot2.tidyverse.org/reference/scale_viridis.html) are designed to still be perceptible in black-and-white, as well as for those with colourblindness. It comes with five colourscales, selected using the option= parameter: "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"), "viridis" (or "D", the default), and "cividis" (or "E"). ```r +library(plotly) +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) + p <- ggplot(beers, aes(x=abv, y=ibu)) + stat_density_2d(aes(fill = stat(level)), geom="polygon") + scale_fill_viridis_c(option = "plasma") + @@ -106,6 +113,9 @@ chart_link You can also set your own colour gradients by defining a high and low point. ```r +library(plotly) +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) + p <- ggplot(beers, aes(x=abv, y=ibu)) + stat_density_2d(aes(fill = stat(level)), geom="polygon") + scale_fill_gradient(low = "lightskyblue1", high = "darkred") + @@ -128,7 +138,9 @@ I use variable "style2" to filter out the six most common beer styles. This way, ```r +library(plotly) library(dplyr) +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + geom_density_2d(alpha=0.5) + From c5b54dd0277d467d26b44ddceed2769dbfe199d6 Mon Sep 17 00:00:00 2001 From: Koji Shiromoto Date: Mon, 26 Aug 2019 18:33:59 -0400 Subject: [PATCH 3/3] got rid of the underscore for aesthetic reasons (function works both with and without the underscore) --- ...y_2d.Rmd => 2019-08-06-geom_density2d.Rmd} | 30 +++++++------- ...ity_2d.md => 2019-08-06-geom_density2d.md} | 41 ++++++++++--------- 2 files changed, 36 insertions(+), 35 deletions(-) rename _posts/ggplot2/{2019-08-06-geom_density_2d.Rmd => 2019-08-06-geom_density2d.Rmd} (86%) rename _posts/ggplot2/{2019-08-06-geom_density_2d.md => 2019-08-06-geom_density2d.md} (81%) diff --git a/_posts/ggplot2/2019-08-06-geom_density_2d.Rmd b/_posts/ggplot2/2019-08-06-geom_density2d.Rmd similarity index 86% rename from _posts/ggplot2/2019-08-06-geom_density_2d.Rmd rename to _posts/ggplot2/2019-08-06-geom_density2d.Rmd index bfa2ebf51d44..aa851ce69fac 100644 --- a/_posts/ggplot2/2019-08-06-geom_density_2d.Rmd +++ b/_posts/ggplot2/2019-08-06-geom_density2d.Rmd @@ -1,10 +1,10 @@ --- -title: geom_density_2d | Examples | Plotly -name: geom_density_2d -permalink: ggplot2/geom_density_2d/ -description: How to make a density map using geom_density_2d. +title: geom_density2d | Examples | Plotly +name: geom_density2d +permalink: ggplot2/geom_density2d/ +description: How to make a density map using geom_density2d. layout: base -thumbnail: thumbnail/geom_density_2d.jpg +thumbnail: thumbnail/geom_density2d.jpg language: ggplot2 page_type: example_index has_thumbnail: true @@ -46,7 +46,7 @@ library(plotly) beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + - geom_density_2d() + + geom_density2d() + labs(y = "bitterness (IBU)", x = "alcohol volume (ABV)", title = "Craft beers from American breweries") @@ -54,7 +54,7 @@ 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_density_2d/basic-graph") +chart_link = api_create(p, filename="geom_density2d/basic-graph") chart_link ``` @@ -70,7 +70,7 @@ library(plotly) beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + - stat_density_2d(aes(fill = stat(level)), geom="polygon") + + stat_density2d(aes(fill = stat(level)), geom="polygon") + labs(y = "bitterness (IBU)", x = "alcohol volume (ABV)", title = "Craft beers from American breweries") @@ -78,7 +78,7 @@ 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_density_2d/filled") +chart_link = api_create(p, filename="geom_density2d/filled") chart_link ``` @@ -94,7 +94,7 @@ library(plotly) beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + - stat_density_2d(aes(fill = stat(level)), geom="polygon") + + stat_density2d(aes(fill = stat(level)), geom="polygon") + scale_fill_viridis_c(option = "plasma") + theme(legend.position = "magma") + labs(y = "bitterness (IBU)", @@ -104,7 +104,7 @@ 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_density_2d/preset-colours") +chart_link = api_create(p, filename="geom_density2d/preset-colours") chart_link ``` @@ -119,7 +119,7 @@ library(plotly) beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + - stat_density_2d(aes(fill = stat(level)), geom="polygon") + + stat_density2d(aes(fill = stat(level)), geom="polygon") + scale_fill_gradient(low = "lightskyblue1", high = "darkred") + theme(legend.position = "none") + labs(y = "bitterness (IBU)", @@ -129,7 +129,7 @@ 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_density_2d/customized-colours") +chart_link = api_create(p, filename="geom_density2d/customized-colours") chart_link ``` @@ -146,7 +146,7 @@ library(dplyr) beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + - geom_density_2d(alpha=0.5) + + geom_density2d(alpha=0.5) + geom_point(data=filter(beers, !is.na(style2)), aes(colour=style2, text = label), alpha=0.3) + labs(y = "bitterness (IBU)", x = "alcohol volume (ABV)", @@ -156,7 +156,7 @@ 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_density_2d/overlaid-points") +chart_link = api_create(p, filename="geom_density2d/overlaid-points") chart_link ``` diff --git a/_posts/ggplot2/2019-08-06-geom_density_2d.md b/_posts/ggplot2/2019-08-06-geom_density2d.md similarity index 81% rename from _posts/ggplot2/2019-08-06-geom_density_2d.md rename to _posts/ggplot2/2019-08-06-geom_density2d.md index 09c1d479617b..0a274d74bead 100644 --- a/_posts/ggplot2/2019-08-06-geom_density_2d.md +++ b/_posts/ggplot2/2019-08-06-geom_density2d.md @@ -1,10 +1,10 @@ --- -title: geom_density_2d | Examples | Plotly -name: geom_density_2d -permalink: ggplot2/geom_density_2d/ -description: How to make a density map using geom_density_2d. +title: geom_density2d | Examples | Plotly +name: geom_density2d +permalink: ggplot2/geom_density2d/ +description: How to make a density map using geom_density2d. layout: base -thumbnail: thumbnail/geom_density_2d.jpg +thumbnail: thumbnail/geom_density2d.jpg language: ggplot2 page_type: example_index has_thumbnail: true @@ -48,7 +48,7 @@ library(plotly) beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + - geom_density_2d() + + geom_density2d() + labs(y = "bitterness (IBU)", x = "alcohol volume (ABV)", title = "Craft beers from American breweries") @@ -56,11 +56,11 @@ 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_density_2d/basic-graph") +chart_link = api_create(p, filename="geom_density2d/basic-graph") chart_link ``` - + ### Filled Since each of the lines (in the above graph) shows a different "level", setting "fill = stat(level)" allows for a filled graph. @@ -71,7 +71,7 @@ library(plotly) beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + - stat_density_2d(aes(fill = stat(level)), geom="polygon") + + stat_density2d(aes(fill = stat(level)), geom="polygon") + labs(y = "bitterness (IBU)", x = "alcohol volume (ABV)", title = "Craft beers from American breweries") @@ -79,21 +79,22 @@ 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_density_2d/filled") +chart_link = api_create(p, filename="geom_density2d/filled") chart_link ``` - + ### Preset Colourscale ["Viridis" colourscales](https://ggplot2.tidyverse.org/reference/scale_viridis.html) are designed to still be perceptible in black-and-white, as well as for those with colourblindness. It comes with five colourscales, selected using the option= parameter: "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"), "viridis" (or "D", the default), and "cividis" (or "E"). + ```r library(plotly) beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + - stat_density_2d(aes(fill = stat(level)), geom="polygon") + + stat_density2d(aes(fill = stat(level)), geom="polygon") + scale_fill_viridis_c(option = "plasma") + theme(legend.position = "magma") + labs(y = "bitterness (IBU)", @@ -103,11 +104,11 @@ 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_density_2d/preset-colours") +chart_link = api_create(p, filename="geom_density2d/preset-colours") chart_link ``` - + ### Customized Colourscale You can also set your own colour gradients by defining a high and low point. @@ -117,7 +118,7 @@ library(plotly) beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + - stat_density_2d(aes(fill = stat(level)), geom="polygon") + + stat_density2d(aes(fill = stat(level)), geom="polygon") + scale_fill_gradient(low = "lightskyblue1", high = "darkred") + theme(legend.position = "none") + labs(y = "bitterness (IBU)", @@ -127,11 +128,11 @@ 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_density_2d/customized-colours") +chart_link = api_create(p, filename="geom_density2d/customized-colours") chart_link ``` - + ### Overlaid Points I use variable "style2" to filter out the six most common beer styles. This way, we can see that the cluster of beers in the top right (i.e. more bitter and higher alcohol content) are IPAs - perhaps unsurprisingly. @@ -143,7 +144,7 @@ library(dplyr) beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) p <- ggplot(beers, aes(x=abv, y=ibu)) + - geom_density_2d(alpha=0.5) + + geom_density2d(alpha=0.5) + geom_point(data=filter(beers, !is.na(style2)), aes(colour=style2, text = label), alpha=0.3) + labs(y = "bitterness (IBU)", x = "alcohol volume (ABV)", @@ -153,9 +154,9 @@ 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_density_2d/overlaid-points") +chart_link = api_create(p, filename="geom_density2d/overlaid-points") chart_link ``` - +