Skip to content

Commit f60fdd8

Browse files
authored
Merge pull request #1434 from plotly/geom_hex
added geom_hex
2 parents dadd12c + 784241b commit f60fdd8

File tree

5 files changed

+316
-8
lines changed

5 files changed

+316
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ vendor/bundle
2222
_posts/python/fundamentals/static-image/images
2323
_config_personal.yml
2424
_posts/python/html
25+
.Rproj.user
26+
documentation.Rproj

_posts/ggplot2/2019-07-12-geom_bin2d.Rmd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ packageVersion('plotly')
3939
```
4040

4141
### Basic 2d Heatmap
42+
See also geom\_hex for a similar geom with hexagonal bins. Note: facetting is supported in geom\_bin2d but not geom\_hex.
43+
44+
Source: [Department of Canadian Heritage](https://open.canada.ca/data/en/dataset/a0bff264-1c80-41ee-aef9-e7da347c5158)
4245

4346
```{r, results='hide'}
4447
library(plotly)
@@ -100,7 +103,7 @@ p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
100103
labs(title = "Distribution of the Canadian population by English and French fluency",
101104
x = "% fluent in English",
102105
y = "% fluent in French",
103-
fill = "# of people")
106+
fill = "population")
104107
p <- ggplotly(p)
105108
106109
@@ -127,7 +130,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
127130
labs(title = "Distribution of Canadian towns by English and French fluency",
128131
x = "% fluent in English",
129132
y = "% fluent in French",
130-
fill = "# of people")
133+
fill = "population")
131134
p <- ggplotly(p)
132135
133136
@@ -154,7 +157,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
154157
labs(title = "Distribution of Canadian towns by English and French fluency",
155158
x = "% fluent in English",
156159
y = "% fluent in French",
157-
fill = "# of people") +
160+
fill = "population") +
158161
theme_bw() +
159162
theme(text = element_text(family = 'Fira Sans'))
160163
p <- ggplotly(p)

_posts/ggplot2/2019-07-12-geom_bin2d.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ packageVersion('plotly')
3636
```
3737

3838
```
39-
## [1] '4.9.0'
39+
## [1] '4.9.0.9000'
4040
```
4141

4242
### Basic 2d Heatmap
43+
See also [geom_hex](https://plot.ly/ggplot2/geom_hex/) for a similar geom with hexagonal bins. Note: facetting is supported in geom\_bin2d but not geom\_hex.
4344

45+
Source: [Department of Canadian Heritage](https://open.canada.ca/data/en/dataset/a0bff264-1c80-41ee-aef9-e7da347c5158)
4446

4547
```r
4648
library(plotly)
@@ -100,7 +102,7 @@ p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
100102
labs(title = "Distribution of the Canadian population by English and French fluency",
101103
x = "% fluent in English",
102104
y = "% fluent in French",
103-
fill = "# of people")
105+
fill = "population")
104106
p <- ggplotly(p)
105107

106108

@@ -113,7 +115,7 @@ chart_link
113115
<iframe src="https://plot.ly/~RPlotBot/5680.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
114116

115117
### With Facets
116-
We can facet the graphic with the "region" column, and set "bins" to 20, so that the graph is 20 x 20 sides.
118+
We can facet the graphic with the "region" column, and set "bins" to 20, so that the graph is 20 x 20 sides.
117119

118120

119121
```r
@@ -126,7 +128,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
126128
labs(title = "Distribution of Canadian towns by English and French fluency",
127129
x = "% fluent in English",
128130
y = "% fluent in French",
129-
fill = "# of people")
131+
fill = "population")
130132
p <- ggplotly(p)
131133

132134

@@ -152,7 +154,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
152154
labs(title = "Distribution of Canadian towns by English and French fluency",
153155
x = "% fluent in English",
154156
y = "% fluent in French",
155-
fill = "# of people") +
157+
fill = "population") +
156158
theme_bw() +
157159
theme(text = element_text(family = 'Fira Sans'))
158160
p <- ggplotly(p)
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: geom_hex | Examples | Plotly
3+
name: geom_hex
4+
permalink: ggplot2/geom_hex/
5+
description: How to make a hexagonal two-dimensional heatmap in ggplot2 using geom_hex. Examples of coloured and facetted graphs.
6+
layout: base
7+
thumbnail: thumbnail/geom_hex.jpg
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: statistical
12+
order: 3
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 Heatmap
42+
See also [geom_bin2d](https://plot.ly/ggplot2/geom_bin2d/) for a similar geom with rectangular bins. Note: facetting is supported in geom\_bin2d but not geom\_hex.
43+
44+
Source: [Department of Canadian Heritage](https://open.canada.ca/data/en/dataset/a0bff264-1c80-41ee-aef9-e7da347c5158)
45+
46+
```{r, results='hide'}
47+
library(plotly)
48+
49+
english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)
50+
51+
p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
52+
geom_hex() +
53+
labs(title = "Distribution of Canadian areas by English and French fluency",
54+
x = "% fluent in English",
55+
y = "% fluent in French",
56+
fill = "# of census \nsubdivisions")
57+
p <- ggplotly(p)
58+
59+
# Create a shareable link to your chart
60+
# Set up API credentials: https://plot.ly/r/getting-started
61+
chart_link = api_create(p, filename="geom_hex/2d-chart")
62+
chart_link
63+
```
64+
65+
```{r echo=FALSE}
66+
chart_link
67+
```
68+
69+
### Customized Colours
70+
Let's flip the colour scheme so that lighter colours denote larger numbers than darker colours. We should also move to a logarithmic scale, since as it is, the very large value in the bottom right overshadows all other values.
71+
72+
```{r, results='hide'}
73+
library(plotly)
74+
75+
english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)
76+
77+
p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
78+
geom_hex() +
79+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
80+
labs(title = "Distribution of Canadian towns by English and French fluency",
81+
x = "% fluent in English",
82+
y = "% fluent in French",
83+
fill = "# of census \nsubdivisions")
84+
p <- ggplotly(p)
85+
86+
# Create a shareable link to your chart
87+
# Set up API credentials: https://plot.ly/r/getting-started
88+
chart_link = api_create(p, filename="geom_hex/log-chart")
89+
chart_link
90+
```
91+
92+
```{r echo=FALSE}
93+
chart_link
94+
```
95+
96+
### Weighted Data
97+
In the previous graphs, each observation represented a single census subdivision - this counted small towns of 500 people equally with cities like Montreal and Toronto. We can weight the data by the "total" column (i.e. total population) to make this a graph of population.
98+
99+
```{r, results='hide'}
100+
library(plotly)
101+
102+
english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)
103+
104+
p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
105+
geom_hex() +
106+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
107+
labs(title = "Distribution of the Canadian population by English and French fluency",
108+
x = "% fluent in English",
109+
y = "% fluent in French",
110+
fill = "population")
111+
ggplotly(p)
112+
113+
114+
# Create a shareable link to your chart
115+
# Set up API credentials: https://plot.ly/r/getting-started
116+
chart_link = api_create(p, filename="geom_hex/weighted-data")
117+
chart_link
118+
```
119+
120+
```{r echo=FALSE}
121+
chart_link
122+
```
123+
124+
### Customized Appearance
125+
We can modify the graph's appearance - for example, if the grey background makes it difficult to make out the paler shades of blue, we can change the theme to one with a white background. Included also is a way to change the font. You can find a list [here](http://ggplot2.tidyverse.org/reference/theme.html) of all the theme elements that you can modify.
126+
127+
```{r, results='hide'}
128+
library(plotly)
129+
130+
english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)
131+
132+
p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
133+
geom_hex(bins = 20) +
134+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
135+
labs(title = "Distribution of Canadian towns by English and French fluency",
136+
x = "% fluent in English",
137+
y = "% fluent in French",
138+
fill = "population") +
139+
theme_bw() +
140+
theme(text = element_text(family = 'Fira Sans'))
141+
p <- ggplotly(p)
142+
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_hex/customize-theme")
147+
chart_link
148+
```
149+
150+
```{r echo=FALSE}
151+
chart_link
152+
```
153+

_posts/ggplot2/2019-07-30-geom_hex.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
---
2+
title: geom_hex | Examples | Plotly
3+
name: geom_hex
4+
permalink: ggplot2/geom_hex/
5+
description: How to make a hexagonal two-dimensional heatmap in ggplot2 using geom_hex. Examples of coloured and facetted graphs.
6+
layout: base
7+
thumbnail: thumbnail/geom_hex.jpg
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: statistical
12+
order: 3
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.9.0.9000'
40+
```
41+
42+
### Basic 2d Heatmap
43+
See also [geom_bin2d](https://plot.ly/ggplot2/geom_bin2d/) for a similar geom with rectangular bins. Note: facetting is supported in geom\_bin2d but not geom\_hex.
44+
45+
Source: [Department of Canadian Heritage](https://open.canada.ca/data/en/dataset/a0bff264-1c80-41ee-aef9-e7da347c5158)
46+
47+
```r
48+
library(plotly)
49+
50+
english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)
51+
52+
p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
53+
geom_hex() +
54+
labs(title = "Distribution of Canadian areas by English and French fluency",
55+
x = "% fluent in English",
56+
y = "% fluent in French",
57+
fill = "# of census \nsubdivisions")
58+
p <- ggplotly(p)
59+
60+
# Create a shareable link to your chart
61+
# Set up API credentials: https://plot.ly/r/getting-started
62+
chart_link = api_create(p, filename="geom_hex/2d-chart")
63+
chart_link
64+
```
65+
66+
<iframe src="https://plot.ly/~RPlotBot/5729.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
67+
68+
### Customized Colours
69+
Let's flip the colour scheme so that lighter colours denote larger numbers than darker colours. We should also move to a logarithmic scale, since as it is, the very large value in the bottom right overshadows all other values.
70+
71+
72+
```r
73+
library(plotly)
74+
75+
english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)
76+
77+
p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
78+
geom_hex() +
79+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
80+
labs(title = "Distribution of Canadian towns by English and French fluency",
81+
x = "% fluent in English",
82+
y = "% fluent in French",
83+
fill = "# of census \nsubdivisions")
84+
p <- ggplotly(p)
85+
86+
# Create a shareable link to your chart
87+
# Set up API credentials: https://plot.ly/r/getting-started
88+
chart_link = api_create(p, filename="geom_hex/log-chart")
89+
chart_link
90+
```
91+
92+
<iframe src="https://plot.ly/~RPlotBot/5731.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
93+
94+
### Weighted Data
95+
In the previous graphs, each observation represented a single census subdivision - this counted small towns of 500 people equally with cities like Montreal and Toronto. We can weight the data by the "total" column (i.e. total population) to make this a graph of population.
96+
97+
98+
```r
99+
library(plotly)
100+
101+
english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)
102+
103+
p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
104+
geom_hex() +
105+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
106+
labs(title = "Distribution of the Canadian population by English and French fluency",
107+
x = "% fluent in English",
108+
y = "% fluent in French",
109+
fill = "population")
110+
ggplotly(p)
111+
112+
# Create a shareable link to your chart
113+
# Set up API credentials: https://plot.ly/r/getting-started
114+
chart_link = api_create(p, filename="geom_hex/weighted-data")
115+
chart_link
116+
```
117+
118+
<iframe src="https://plot.ly/~RPlotBot/5733.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
119+
120+
### Customized Appearance
121+
We can modify the graph's appearance - for example, if the grey background makes it difficult to make out the paler shades of blue, we can change the theme to one with a white background. Included also is a way to change the font. You can find a list [here](http://ggplot2.tidyverse.org/reference/theme.html) of all the theme elements that you can modify.
122+
123+
124+
```r
125+
library(plotly)
126+
127+
english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)
128+
129+
p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
130+
geom_hex(bins = 20) +
131+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
132+
labs(title = "Distribution of Canadian towns by English and French fluency",
133+
x = "% fluent in English",
134+
y = "% fluent in French",
135+
fill = "population") +
136+
theme_bw() +
137+
theme(text = element_text(family = 'Fira Sans'))
138+
p <- ggplotly(p)
139+
140+
141+
# Create a shareable link to your chart
142+
# Set up API credentials: https://plot.ly/r/getting-started
143+
chart_link = api_create(p, filename="geom_hex/customize-theme")
144+
chart_link
145+
```
146+
147+
<iframe src="https://plot.ly/~RPlotBot/5739.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
148+

0 commit comments

Comments
 (0)