|
| 1 | +--- |
| 2 | +title: geom_density2d | Examples | Plotly |
| 3 | +name: geom_density2d |
| 4 | +permalink: ggplot2/geom_density2d/ |
| 5 | +description: How to make a density map using geom_density2d. |
| 6 | +layout: base |
| 7 | +thumbnail: thumbnail/geom_density2d.jpg |
| 8 | +language: ggplot2 |
| 9 | +page_type: example_index |
| 10 | +has_thumbnail: true |
| 11 | +display_as: statistical |
| 12 | +order: 8 |
| 13 | +output: |
| 14 | + html_document: |
| 15 | + keep_md: true |
| 16 | +--- |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +### New to Plotly? |
| 21 | + |
| 22 | +Plotly's R library is free and open source!<br> |
| 23 | +[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br> |
| 24 | +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.<br> |
| 25 | +We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started! |
| 26 | + |
| 27 | +### Version Check |
| 28 | + |
| 29 | +Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br> |
| 30 | +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. |
| 31 | + |
| 32 | + |
| 33 | +```r |
| 34 | +library(plotly) |
| 35 | +packageVersion('plotly') |
| 36 | +``` |
| 37 | + |
| 38 | +``` |
| 39 | +## [1] '4.8.0.9000' |
| 40 | +``` |
| 41 | + |
| 42 | +### Basic 2D Graph |
| 43 | +Source: [Brett Carpenter from Data.World](https://data.world/brettcarpenter/craft-beer-data) |
| 44 | + |
| 45 | + |
| 46 | +```r |
| 47 | +library(plotly) |
| 48 | +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) |
| 49 | + |
| 50 | +p <- ggplot(beers, aes(x=abv, y=ibu)) + |
| 51 | + geom_density2d() + |
| 52 | + labs(y = "bitterness (IBU)", |
| 53 | + x = "alcohol volume (ABV)", |
| 54 | + title = "Craft beers from American breweries") |
| 55 | +ggplotly(p) |
| 56 | + |
| 57 | +# Create a shareable link to your chart |
| 58 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 59 | +chart_link = api_create(p, filename="geom_density2d/basic-graph") |
| 60 | +chart_link |
| 61 | +``` |
| 62 | + |
| 63 | +<iframe src="https://plot.ly/~RPlotBot/5855.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe> |
| 64 | + |
| 65 | +### Filled |
| 66 | +Since each of the lines (in the above graph) shows a different "level", setting "fill = stat(level)" allows for a filled graph. |
| 67 | + |
| 68 | + |
| 69 | +```r |
| 70 | +library(plotly) |
| 71 | +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) |
| 72 | + |
| 73 | +p <- ggplot(beers, aes(x=abv, y=ibu)) + |
| 74 | + stat_density2d(aes(fill = stat(level)), geom="polygon") + |
| 75 | + labs(y = "bitterness (IBU)", |
| 76 | + x = "alcohol volume (ABV)", |
| 77 | + title = "Craft beers from American breweries") |
| 78 | +ggplotly(p) |
| 79 | + |
| 80 | +# Create a shareable link to your chart |
| 81 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 82 | +chart_link = api_create(p, filename="geom_density2d/filled") |
| 83 | +chart_link |
| 84 | +``` |
| 85 | + |
| 86 | +<iframe src="https://plot.ly/~RPlotBot/5857.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe> |
| 87 | + |
| 88 | +### Preset Colourscale |
| 89 | +["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"). |
| 90 | + |
| 91 | + |
| 92 | +```r |
| 93 | +library(plotly) |
| 94 | +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) |
| 95 | + |
| 96 | +p <- ggplot(beers, aes(x=abv, y=ibu)) + |
| 97 | + stat_density2d(aes(fill = stat(level)), geom="polygon") + |
| 98 | + scale_fill_viridis_c(option = "plasma") + |
| 99 | + theme(legend.position = "magma") + |
| 100 | + labs(y = "bitterness (IBU)", |
| 101 | + x = "alcohol volume (ABV)", |
| 102 | + title = "Craft beers from American breweries") |
| 103 | +ggplotly(p) |
| 104 | + |
| 105 | +# Create a shareable link to your chart |
| 106 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 107 | +chart_link = api_create(p, filename="geom_density2d/preset-colours") |
| 108 | +chart_link |
| 109 | +``` |
| 110 | + |
| 111 | +<iframe src="https://plot.ly/~RPlotBot/5859.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe> |
| 112 | + |
| 113 | +### Customized Colourscale |
| 114 | +You can also set your own colour gradients by defining a high and low point. |
| 115 | + |
| 116 | +```r |
| 117 | +library(plotly) |
| 118 | +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) |
| 119 | + |
| 120 | +p <- ggplot(beers, aes(x=abv, y=ibu)) + |
| 121 | + stat_density2d(aes(fill = stat(level)), geom="polygon") + |
| 122 | + scale_fill_gradient(low = "lightskyblue1", high = "darkred") + |
| 123 | + theme(legend.position = "none") + |
| 124 | + labs(y = "bitterness (IBU)", |
| 125 | + x = "alcohol volume (ABV)", |
| 126 | + title = "Craft beers from American breweries") |
| 127 | +ggplotly(p) |
| 128 | + |
| 129 | +# Create a shareable link to your chart |
| 130 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 131 | +chart_link = api_create(p, filename="geom_density2d/customized-colours") |
| 132 | +chart_link |
| 133 | +``` |
| 134 | + |
| 135 | +<iframe src="https://plot.ly/~RPlotBot/5861.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe> |
| 136 | + |
| 137 | +### Overlaid Points |
| 138 | +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. |
| 139 | + |
| 140 | + |
| 141 | +```r |
| 142 | +library(plotly) |
| 143 | +library(dplyr) |
| 144 | +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) |
| 145 | + |
| 146 | +p <- ggplot(beers, aes(x=abv, y=ibu)) + |
| 147 | + geom_density2d(alpha=0.5) + |
| 148 | + geom_point(data=filter(beers, !is.na(style2)), aes(colour=style2, text = label), alpha=0.3) + |
| 149 | + labs(y = "bitterness (IBU)", |
| 150 | + x = "alcohol volume (ABV)", |
| 151 | + title = "Craft beers from American breweries", |
| 152 | + colour = "Beer types") |
| 153 | +ggplotly(p) |
| 154 | + |
| 155 | +# Create a shareable link to your chart |
| 156 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 157 | +chart_link = api_create(p, filename="geom_density2d/overlaid-points") |
| 158 | +chart_link |
| 159 | +``` |
| 160 | + |
| 161 | +<iframe src="https://plot.ly/~RPlotBot/5863.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe> |
| 162 | + |
0 commit comments