Skip to content

Commit 64af07c

Browse files
authored
Merge pull request #1445 from plotly/geom_density_2d
geom_density_2d
2 parents ba18717 + c5b54dd commit 64af07c

File tree

2 files changed

+328
-0
lines changed

2 files changed

+328
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
```{r, echo = FALSE, message=FALSE}
19+
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
20+
Sys.setenv("plotly_username"="RPlotBot")
21+
Sys.setenv("plotly_api_key"="q0lz6r5efr")
22+
```
23+
24+
### New to Plotly?
25+
26+
Plotly's R library is free and open source!<br>
27+
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
28+
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>
29+
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!
30+
31+
### Version Check
32+
33+
Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
34+
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.
35+
36+
```{r}
37+
library(plotly)
38+
packageVersion('plotly')
39+
```
40+
41+
### Basic 2D Graph
42+
Source: [Brett Carpenter from Data.World](https://data.world/brettcarpenter/craft-beer-data)
43+
44+
```{r, results='hide'}
45+
library(plotly)
46+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
47+
48+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
49+
geom_density2d() +
50+
labs(y = "bitterness (IBU)",
51+
x = "alcohol volume (ABV)",
52+
title = "Craft beers from American breweries")
53+
ggplotly(p)
54+
55+
# Create a shareable link to your chart
56+
# Set up API credentials: https://plot.ly/r/getting-started
57+
chart_link = api_create(p, filename="geom_density2d/basic-graph")
58+
chart_link
59+
```
60+
61+
```{r echo=FALSE}
62+
chart_link
63+
```
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+
```{r, results='hide'}
69+
library(plotly)
70+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
71+
72+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
73+
stat_density2d(aes(fill = stat(level)), geom="polygon") +
74+
labs(y = "bitterness (IBU)",
75+
x = "alcohol volume (ABV)",
76+
title = "Craft beers from American breweries")
77+
ggplotly(p)
78+
79+
# Create a shareable link to your chart
80+
# Set up API credentials: https://plot.ly/r/getting-started
81+
chart_link = api_create(p, filename="geom_density2d/filled")
82+
chart_link
83+
```
84+
85+
```{r echo=FALSE}
86+
chart_link
87+
```
88+
89+
### Preset Colourscale
90+
["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").
91+
92+
```{r, results='hide'}
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+
```{r echo=FALSE}
112+
chart_link
113+
```
114+
115+
### Customized Colourscale
116+
You can also set your own colour gradients by defining a high and low point.
117+
```{r, results='hide'}
118+
library(plotly)
119+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
120+
121+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
122+
stat_density2d(aes(fill = stat(level)), geom="polygon") +
123+
scale_fill_gradient(low = "lightskyblue1", high = "darkred") +
124+
theme(legend.position = "none") +
125+
labs(y = "bitterness (IBU)",
126+
x = "alcohol volume (ABV)",
127+
title = "Craft beers from American breweries")
128+
ggplotly(p)
129+
130+
# Create a shareable link to your chart
131+
# Set up API credentials: https://plot.ly/r/getting-started
132+
chart_link = api_create(p, filename="geom_density2d/customized-colours")
133+
chart_link
134+
```
135+
136+
```{r echo=FALSE}
137+
chart_link
138+
```
139+
140+
### Overlaid Points
141+
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.
142+
143+
```{r, results='hide'}
144+
library(plotly)
145+
library(dplyr)
146+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
147+
148+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
149+
geom_density2d(alpha=0.5) +
150+
geom_point(data=filter(beers, !is.na(style2)), aes(colour=style2, text = label), alpha=0.3) +
151+
labs(y = "bitterness (IBU)",
152+
x = "alcohol volume (ABV)",
153+
title = "Craft beers from American breweries",
154+
colour = "Beer types")
155+
ggplotly(p)
156+
157+
# Create a shareable link to your chart
158+
# Set up API credentials: https://plot.ly/r/getting-started
159+
chart_link = api_create(p, filename="geom_density2d/overlaid-points")
160+
chart_link
161+
```
162+
163+
```{r echo=FALSE}
164+
chart_link
165+
```
166+
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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

Comments
 (0)