|
| 1 | +--- |
| 2 | +name: 2D-Histogram |
| 3 | +permalink: ggplot2/geom_density2d/ |
| 4 | +description: How to make 2D-Histogram Plots plots in ggplot2 with Plotly. |
| 5 | +layout: base |
| 6 | +thumbnail: thumbnail/histogram2d.jpg |
| 7 | +language: ggplot2 |
| 8 | +page_type: u-guide |
| 9 | +display_as: statistical |
| 10 | +order: 5 |
| 11 | +output: |
| 12 | + html_document: |
| 13 | + keep_md: true |
| 14 | +--- |
| 15 | + |
| 16 | +```{r, echo = FALSE, message=FALSE} |
| 17 | +knitr::opts_chunk$set(message = FALSE, warning=FALSE) |
| 18 | +``` |
| 19 | +### Basic 2D Graph |
| 20 | +Source: [Brett Carpenter from Data.World](https://data.world/brettcarpenter/craft-beer-data) |
| 21 | + |
| 22 | +```{r} |
| 23 | +library(plotly) |
| 24 | +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) |
| 25 | +
|
| 26 | +p <- ggplot(beers, aes(x=abv, y=ibu)) + |
| 27 | + geom_density2d() + |
| 28 | + labs(y = "bitterness (IBU)", |
| 29 | + x = "alcohol volume (ABV)", |
| 30 | + title = "Craft beers from American breweries") |
| 31 | +
|
| 32 | +ggplotly(p) |
| 33 | +``` |
| 34 | + |
| 35 | +### Filled |
| 36 | +Since each of the lines (in the above graph) shows a different "level", setting "fill = stat(level)" allows for a filled graph. |
| 37 | + |
| 38 | +```{r} |
| 39 | +library(plotly) |
| 40 | +
|
| 41 | +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) |
| 42 | +
|
| 43 | +p <- ggplot(beers, aes(x=abv, y=ibu)) + |
| 44 | + stat_density2d(aes(fill = stat(level)), geom="polygon") + |
| 45 | + labs(y = "bitterness (IBU)", |
| 46 | + x = "alcohol volume (ABV)", |
| 47 | + title = "Craft beers from American breweries") |
| 48 | +
|
| 49 | +ggplotly(p) |
| 50 | +``` |
| 51 | + |
| 52 | +### Preset Colourscale |
| 53 | +["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"). |
| 54 | + |
| 55 | +```{r} |
| 56 | +library(plotly) |
| 57 | +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) |
| 58 | +
|
| 59 | +p <- ggplot(beers, aes(x=abv, y=ibu)) + |
| 60 | + stat_density2d(aes(fill = stat(level)), geom="polygon") + |
| 61 | + scale_fill_viridis_c(option = "plasma") + |
| 62 | + theme(legend.position = "magma") + |
| 63 | + labs(y = "bitterness (IBU)", |
| 64 | + x = "alcohol volume (ABV)", |
| 65 | + title = "Craft beers from American breweries") |
| 66 | +
|
| 67 | +ggplotly(p) |
| 68 | +``` |
| 69 | + |
| 70 | +### Customized Colourscale |
| 71 | +You can also set your own colour gradients by defining a high and low point. |
| 72 | +```{r} |
| 73 | +library(plotly) |
| 74 | +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) |
| 75 | +
|
| 76 | +p <- ggplot(beers, aes(x=abv, y=ibu)) + |
| 77 | + stat_density2d(aes(fill = stat(level)), geom="polygon") + |
| 78 | + scale_fill_gradient(low = "lightskyblue1", high = "darkred") + |
| 79 | + theme(legend.position = "none") + |
| 80 | + labs(y = "bitterness (IBU)", |
| 81 | + x = "alcohol volume (ABV)", |
| 82 | + title = "Craft beers from American breweries") |
| 83 | +
|
| 84 | +ggplotly(p) |
| 85 | +``` |
| 86 | + |
| 87 | +### Overlaid Points |
| 88 | +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. |
| 89 | + |
| 90 | +```{r} |
| 91 | +library(plotly) |
| 92 | +library(dplyr) |
| 93 | +beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE) |
| 94 | +
|
| 95 | +p <- ggplot(beers, aes(x=abv, y=ibu)) + |
| 96 | + geom_density2d(alpha=0.5) + |
| 97 | + geom_point(data=filter(beers, !is.na(style2)), aes(colour=style2, text = label), alpha=0.3) + |
| 98 | + labs(y = "bitterness (IBU)", |
| 99 | + x = "alcohol volume (ABV)", |
| 100 | + title = "Craft beers from American breweries", |
| 101 | + colour = "Beer types") |
| 102 | +
|
| 103 | +ggplotly(p) |
| 104 | +``` |
| 105 | + |
0 commit comments