1
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 .
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
6
layout : base
7
- thumbnail : thumbnail/geom_density_2d .jpg
7
+ thumbnail : thumbnail/geom_density2d .jpg
8
8
language : ggplot2
9
9
page_type : example_index
10
10
has_thumbnail : true
@@ -48,19 +48,19 @@ library(plotly)
48
48
beers <- read.csv(" https://raw.githubusercontent.com/plotly/datasets/master/beers.csv" , stringsAsFactors = FALSE )
49
49
50
50
p <- ggplot(beers , aes(x = abv , y = ibu )) +
51
- geom_density_2d () +
51
+ geom_density2d () +
52
52
labs(y = " bitterness (IBU)" ,
53
53
x = " alcohol volume (ABV)" ,
54
54
title = " Craft beers from American breweries" )
55
55
ggplotly(p )
56
56
57
57
# Create a shareable link to your chart
58
58
# Set up API credentials: https://plot.ly/r/getting-started
59
- chart_link = api_create(p , filename = " geom_density_2d /basic-graph" )
59
+ chart_link = api_create(p , filename = " geom_density2d /basic-graph" )
60
60
chart_link
61
61
```
62
62
63
- <iframe src =" https://plot.ly/~RPlotBot/5808 .embed " width =" 800 " height =" 600 " id =" igraph " scrolling =" no " seamless =" seamless " frameBorder =" 0 " > </iframe >
63
+ <iframe src =" https://plot.ly/~RPlotBot/5855 .embed " width =" 800 " height =" 600 " id =" igraph " scrolling =" no " seamless =" seamless " frameBorder =" 0 " > </iframe >
64
64
65
65
### Filled
66
66
Since each of the lines (in the above graph) shows a different "level", setting "fill = stat(level)" allows for a filled graph.
@@ -71,29 +71,30 @@ library(plotly)
71
71
beers <- read.csv(" https://raw.githubusercontent.com/plotly/datasets/master/beers.csv" , stringsAsFactors = FALSE )
72
72
73
73
p <- ggplot(beers , aes(x = abv , y = ibu )) +
74
- stat_density_2d (aes(fill = stat(level )), geom = " polygon" ) +
74
+ stat_density2d (aes(fill = stat(level )), geom = " polygon" ) +
75
75
labs(y = " bitterness (IBU)" ,
76
76
x = " alcohol volume (ABV)" ,
77
77
title = " Craft beers from American breweries" )
78
78
ggplotly(p )
79
79
80
80
# Create a shareable link to your chart
81
81
# Set up API credentials: https://plot.ly/r/getting-started
82
- chart_link = api_create(p , filename = " geom_density_2d /filled" )
82
+ chart_link = api_create(p , filename = " geom_density2d /filled" )
83
83
chart_link
84
84
```
85
85
86
- <iframe src =" https://plot.ly/~RPlotBot/5810 .embed " width =" 800 " height =" 600 " id =" igraph " scrolling =" no " seamless =" seamless " frameBorder =" 0 " > </iframe >
86
+ <iframe src =" https://plot.ly/~RPlotBot/5857 .embed " width =" 800 " height =" 600 " id =" igraph " scrolling =" no " seamless =" seamless " frameBorder =" 0 " > </iframe >
87
87
88
88
### Preset Colourscale
89
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
90
91
+
91
92
``` r
92
93
library(plotly )
93
94
beers <- read.csv(" https://raw.githubusercontent.com/plotly/datasets/master/beers.csv" , stringsAsFactors = FALSE )
94
95
95
96
p <- ggplot(beers , aes(x = abv , y = ibu )) +
96
- stat_density_2d (aes(fill = stat(level )), geom = " polygon" ) +
97
+ stat_density2d (aes(fill = stat(level )), geom = " polygon" ) +
97
98
scale_fill_viridis_c(option = " plasma" ) +
98
99
theme(legend.position = " magma" ) +
99
100
labs(y = " bitterness (IBU)" ,
@@ -103,11 +104,11 @@ ggplotly(p)
103
104
104
105
# Create a shareable link to your chart
105
106
# Set up API credentials: https://plot.ly/r/getting-started
106
- chart_link = api_create(p , filename = " geom_density_2d /preset-colours" )
107
+ chart_link = api_create(p , filename = " geom_density2d /preset-colours" )
107
108
chart_link
108
109
```
109
110
110
- <iframe src =" https://plot.ly/~RPlotBot/5812 .embed " width =" 800 " height =" 600 " id =" igraph " scrolling =" no " seamless =" seamless " frameBorder =" 0 " > </iframe >
111
+ <iframe src =" https://plot.ly/~RPlotBot/5859 .embed " width =" 800 " height =" 600 " id =" igraph " scrolling =" no " seamless =" seamless " frameBorder =" 0 " > </iframe >
111
112
112
113
### Customized Colourscale
113
114
You can also set your own colour gradients by defining a high and low point.
@@ -117,7 +118,7 @@ library(plotly)
117
118
beers <- read.csv(" https://raw.githubusercontent.com/plotly/datasets/master/beers.csv" , stringsAsFactors = FALSE )
118
119
119
120
p <- ggplot(beers , aes(x = abv , y = ibu )) +
120
- stat_density_2d (aes(fill = stat(level )), geom = " polygon" ) +
121
+ stat_density2d (aes(fill = stat(level )), geom = " polygon" ) +
121
122
scale_fill_gradient(low = " lightskyblue1" , high = " darkred" ) +
122
123
theme(legend.position = " none" ) +
123
124
labs(y = " bitterness (IBU)" ,
@@ -127,11 +128,11 @@ ggplotly(p)
127
128
128
129
# Create a shareable link to your chart
129
130
# Set up API credentials: https://plot.ly/r/getting-started
130
- chart_link = api_create(p , filename = " geom_density_2d /customized-colours" )
131
+ chart_link = api_create(p , filename = " geom_density2d /customized-colours" )
131
132
chart_link
132
133
```
133
134
134
- <iframe src =" https://plot.ly/~RPlotBot/5814 .embed " width =" 800 " height =" 600 " id =" igraph " scrolling =" no " seamless =" seamless " frameBorder =" 0 " > </iframe >
135
+ <iframe src =" https://plot.ly/~RPlotBot/5861 .embed " width =" 800 " height =" 600 " id =" igraph " scrolling =" no " seamless =" seamless " frameBorder =" 0 " > </iframe >
135
136
136
137
### Overlaid Points
137
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.
@@ -143,7 +144,7 @@ library(dplyr)
143
144
beers <- read.csv(" https://raw.githubusercontent.com/plotly/datasets/master/beers.csv" , stringsAsFactors = FALSE )
144
145
145
146
p <- ggplot(beers , aes(x = abv , y = ibu )) +
146
- geom_density_2d (alpha = 0.5 ) +
147
+ geom_density2d (alpha = 0.5 ) +
147
148
geom_point(data = filter(beers , ! is.na(style2 )), aes(colour = style2 , text = label ), alpha = 0.3 ) +
148
149
labs(y = " bitterness (IBU)" ,
149
150
x = " alcohol volume (ABV)" ,
@@ -153,9 +154,9 @@ ggplotly(p)
153
154
154
155
# Create a shareable link to your chart
155
156
# Set up API credentials: https://plot.ly/r/getting-started
156
- chart_link = api_create(p , filename = " geom_density_2d /overlaid-points" )
157
+ chart_link = api_create(p , filename = " geom_density2d /overlaid-points" )
157
158
chart_link
158
159
```
159
160
160
- <iframe src =" https://plot.ly/~RPlotBot/5816 .embed " width =" 800 " height =" 600 " id =" igraph " scrolling =" no " seamless =" seamless " frameBorder =" 0 " > </iframe >
161
+ <iframe src =" https://plot.ly/~RPlotBot/5863 .embed " width =" 800 " height =" 600 " id =" igraph " scrolling =" no " seamless =" seamless " frameBorder =" 0 " > </iframe >
161
162
0 commit comments