Skip to content

Commit 72bcc6f

Browse files
committed
new geom!
1 parent c17b05a commit 72bcc6f

File tree

2 files changed

+302
-0
lines changed

2 files changed

+302
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: geom_density_2d | Examples | Plotly
3+
name: geom_density_2d
4+
permalink: ggplot2/geom_density_2d/
5+
description: How to make a density map using geom_density_2d.
6+
layout: base
7+
thumbnail: thumbnail/geom_density_2d.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+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
46+
47+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
48+
geom_density_2d() +
49+
labs(y = "bitterness (IBU)",
50+
x = "alcohol volume (ABV)",
51+
title = "Craft beers from American breweries")
52+
ggplotly(p)
53+
54+
# Create a shareable link to your chart
55+
# Set up API credentials: https://plot.ly/r/getting-started
56+
chart_link = api_create(p, filename="geom_density_2d/basic-graph")
57+
chart_link
58+
```
59+
60+
```{r echo=FALSE}
61+
chart_link
62+
```
63+
64+
### Filled
65+
Since each of the lines (in the above graph) shows a different "level", setting "fill = stat(level)" allows for a filled graph.
66+
67+
```{r, results='hide'}
68+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
69+
stat_density_2d(aes(fill = stat(level)), geom="polygon") +
70+
labs(y = "bitterness (IBU)",
71+
x = "alcohol volume (ABV)",
72+
title = "Craft beers from American breweries")
73+
ggplotly(p)
74+
75+
# Create a shareable link to your chart
76+
# Set up API credentials: https://plot.ly/r/getting-started
77+
chart_link = api_create(p, filename="geom_density_2d/filled")
78+
chart_link
79+
```
80+
81+
```{r echo=FALSE}
82+
chart_link
83+
```
84+
85+
### Preset Colourscale
86+
["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").
87+
```{r, results='hide'}
88+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
89+
stat_density_2d(aes(fill = stat(level)), geom="polygon") +
90+
scale_fill_viridis_c(option = "plasma") +
91+
theme(legend.position = "magma") +
92+
labs(y = "bitterness (IBU)",
93+
x = "alcohol volume (ABV)",
94+
title = "Craft beers from American breweries")
95+
ggplotly(p)
96+
97+
# Create a shareable link to your chart
98+
# Set up API credentials: https://plot.ly/r/getting-started
99+
chart_link = api_create(p, filename="geom_density_2d/preset-colours")
100+
chart_link
101+
```
102+
103+
```{r echo=FALSE}
104+
chart_link
105+
```
106+
107+
### Customized Colourscale
108+
You can also set your own colour gradients by defining a high and low point.
109+
```{r, results='hide'}
110+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
111+
stat_density_2d(aes(fill = stat(level)), geom="polygon") +
112+
scale_fill_gradient(low = "lightskyblue1", high = "darkred") +
113+
theme(legend.position = "none") +
114+
labs(y = "bitterness (IBU)",
115+
x = "alcohol volume (ABV)",
116+
title = "Craft beers from American breweries")
117+
ggplotly(p)
118+
119+
# Create a shareable link to your chart
120+
# Set up API credentials: https://plot.ly/r/getting-started
121+
chart_link = api_create(p, filename="geom_density_2d/customized-colours")
122+
chart_link
123+
```
124+
125+
```{r echo=FALSE}
126+
chart_link
127+
```
128+
129+
### Overlaid Points
130+
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.
131+
132+
```{r, results='hide'}
133+
library(dplyr)
134+
135+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
136+
geom_density_2d(alpha=0.5) +
137+
geom_point(data=filter(beers, !is.na(style2)), aes(colour=style2, text = label), alpha=0.3) +
138+
labs(y = "bitterness (IBU)",
139+
x = "alcohol volume (ABV)",
140+
title = "Craft beers from American breweries",
141+
colour = "Beer types")
142+
ggplotly(p)
143+
144+
# Create a shareable link to your chart
145+
# Set up API credentials: https://plot.ly/r/getting-started
146+
chart_link = api_create(p, filename="geom_density_2d/overlaid-points")
147+
chart_link
148+
```
149+
150+
```{r echo=FALSE}
151+
chart_link
152+
```
153+
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
title: geom_density_2d | Examples | Plotly
3+
name: geom_density_2d
4+
permalink: ggplot2/geom_density_2d/
5+
description: How to make a density map using geom_density_2d.
6+
layout: base
7+
thumbnail: thumbnail/geom_density_2d.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+
beers <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/beers.csv", stringsAsFactors = FALSE)
48+
49+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
50+
geom_density_2d() +
51+
labs(y = "bitterness (IBU)",
52+
x = "alcohol volume (ABV)",
53+
title = "Craft beers from American breweries")
54+
ggplotly(p)
55+
56+
# Create a shareable link to your chart
57+
# Set up API credentials: https://plot.ly/r/getting-started
58+
chart_link = api_create(p, filename="geom_density_2d/basic-graph")
59+
chart_link
60+
```
61+
62+
<iframe src="https://plot.ly/~RPlotBot/5808.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
63+
64+
### Filled
65+
Since each of the lines (in the above graph) shows a different "level", setting "fill = stat(level)" allows for a filled graph.
66+
67+
68+
```r
69+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
70+
stat_density_2d(aes(fill = stat(level)), geom="polygon") +
71+
labs(y = "bitterness (IBU)",
72+
x = "alcohol volume (ABV)",
73+
title = "Craft beers from American breweries")
74+
ggplotly(p)
75+
76+
# Create a shareable link to your chart
77+
# Set up API credentials: https://plot.ly/r/getting-started
78+
chart_link = api_create(p, filename="geom_density_2d/filled")
79+
chart_link
80+
```
81+
82+
<iframe src="https://plot.ly/~RPlotBot/5810.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
83+
84+
### Preset Colourscale
85+
["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").
86+
87+
```r
88+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
89+
stat_density_2d(aes(fill = stat(level)), geom="polygon") +
90+
scale_fill_viridis_c(option = "plasma") +
91+
theme(legend.position = "magma") +
92+
labs(y = "bitterness (IBU)",
93+
x = "alcohol volume (ABV)",
94+
title = "Craft beers from American breweries")
95+
ggplotly(p)
96+
97+
# Create a shareable link to your chart
98+
# Set up API credentials: https://plot.ly/r/getting-started
99+
chart_link = api_create(p, filename="geom_density_2d/preset-colours")
100+
chart_link
101+
```
102+
103+
<iframe src="https://plot.ly/~RPlotBot/5812.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
104+
105+
### Customized Colourscale
106+
You can also set your own colour gradients by defining a high and low point.
107+
108+
```r
109+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
110+
stat_density_2d(aes(fill = stat(level)), geom="polygon") +
111+
scale_fill_gradient(low = "lightskyblue1", high = "darkred") +
112+
theme(legend.position = "none") +
113+
labs(y = "bitterness (IBU)",
114+
x = "alcohol volume (ABV)",
115+
title = "Craft beers from American breweries")
116+
ggplotly(p)
117+
118+
# Create a shareable link to your chart
119+
# Set up API credentials: https://plot.ly/r/getting-started
120+
chart_link = api_create(p, filename="geom_density_2d/customized-colours")
121+
chart_link
122+
```
123+
124+
<iframe src="https://plot.ly/~RPlotBot/5814.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
125+
126+
### Overlaid Points
127+
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.
128+
129+
130+
```r
131+
library(dplyr)
132+
133+
p <- ggplot(beers, aes(x=abv, y=ibu)) +
134+
geom_density_2d(alpha=0.5) +
135+
geom_point(data=filter(beers, !is.na(style2)), aes(colour=style2, text = label), alpha=0.3) +
136+
labs(y = "bitterness (IBU)",
137+
x = "alcohol volume (ABV)",
138+
title = "Craft beers from American breweries",
139+
colour = "Beer types")
140+
ggplotly(p)
141+
142+
# Create a shareable link to your chart
143+
# Set up API credentials: https://plot.ly/r/getting-started
144+
chart_link = api_create(p, filename="geom_density_2d/overlaid-points")
145+
chart_link
146+
```
147+
148+
<iframe src="https://plot.ly/~RPlotBot/5816.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
149+

0 commit comments

Comments
 (0)