Skip to content

Commit 9d089fd

Browse files
committed
added geom_hex, fixed problems with geom_bin2d
1 parent 0c1679c commit 9d089fd

File tree

4 files changed

+296
-8
lines changed

4 files changed

+296
-8
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ 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.
4243

4344
```{r, results='hide'}
4445
library(plotly)
@@ -100,7 +101,7 @@ p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
100101
labs(title = "Distribution of the Canadian population by English and French fluency",
101102
x = "% fluent in English",
102103
y = "% fluent in French",
103-
fill = "# of people")
104+
fill = "population")
104105
p <- ggplotly(p)
105106
106107
@@ -127,7 +128,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
127128
labs(title = "Distribution of Canadian towns by English and French fluency",
128129
x = "% fluent in English",
129130
y = "% fluent in French",
130-
fill = "# of people")
131+
fill = "population")
131132
p <- ggplotly(p)
132133
133134
@@ -154,7 +155,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
154155
labs(title = "Distribution of Canadian towns by English and French fluency",
155156
x = "% fluent in English",
156157
y = "% fluent in French",
157-
fill = "# of people") +
158+
fill = "population") +
158159
theme_bw() +
159160
theme(text = element_text(family = 'Fira Sans'))
160161
p <- ggplotly(p)

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ 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

4445

4546
```r
@@ -100,7 +101,7 @@ p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
100101
labs(title = "Distribution of the Canadian population by English and French fluency",
101102
x = "% fluent in English",
102103
y = "% fluent in French",
103-
fill = "# of people")
104+
fill = "population")
104105
p <- ggplotly(p)
105106

106107

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

115116
### 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.
117+
We can facet the graphic with the "region" column, and set "bins" to 20, so that the graph is 20 x 20 sides.
117118

118119

119120
```r
@@ -126,7 +127,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
126127
labs(title = "Distribution of Canadian towns by English and French fluency",
127128
x = "% fluent in English",
128129
y = "% fluent in French",
129-
fill = "# of people")
130+
fill = "population")
130131
p <- ggplotly(p)
131132

132133

@@ -152,7 +153,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
152153
labs(title = "Distribution of Canadian towns by English and French fluency",
153154
x = "% fluent in English",
154155
y = "% fluent in French",
155-
fill = "# of people") +
156+
fill = "population") +
156157
theme_bw() +
157158
theme(text = element_text(family = 'Fira Sans'))
158159
p <- ggplotly(p)
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
```{r, results='hide'}
45+
library(plotly)
46+
47+
english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)
48+
49+
p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
50+
geom_hex() +
51+
labs(title = "Distribution of Canadian areas by English and French fluency",
52+
x = "% fluent in English",
53+
y = "% fluent in French",
54+
fill = "# of census \nsubdivisions")
55+
p <- 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_hex/2d-chart")
60+
chart_link
61+
```
62+
63+
```{r echo=FALSE}
64+
chart_link
65+
```
66+
67+
### Customized Colours
68+
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.
69+
70+
```{r, results='hide'}
71+
library(plotly)
72+
73+
p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
74+
geom_hex() +
75+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
76+
labs(title = "Distribution of Canadian towns by English and French fluency",
77+
x = "% fluent in English",
78+
y = "% fluent in French",
79+
fill = "# of census \nsubdivisions")
80+
p <- ggplotly(p)
81+
82+
# Create a shareable link to your chart
83+
# Set up API credentials: https://plot.ly/r/getting-started
84+
chart_link = api_create(p, filename="geom_hex/log-chart")
85+
chart_link
86+
```
87+
88+
```{r echo=FALSE}
89+
chart_link
90+
```
91+
92+
### Weighted Data
93+
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.
94+
95+
```{r, results='hide'}
96+
library(plotly)
97+
98+
p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
99+
geom_hex() +
100+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
101+
labs(title = "Distribution of the Canadian population by English and French fluency",
102+
x = "% fluent in English",
103+
y = "% fluent in French",
104+
fill = "population")
105+
ggplotly(p)
106+
107+
108+
# Create a shareable link to your chart
109+
# Set up API credentials: https://plot.ly/r/getting-started
110+
chart_link = api_create(p, filename="geom_hex/weighted-data")
111+
chart_link
112+
```
113+
114+
```{r echo=FALSE}
115+
chart_link
116+
```
117+
118+
### Customized Appearance
119+
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.
120+
121+
```{r, results='hide'}
122+
library(plotly)
123+
124+
p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
125+
geom_hex(bins = 20) +
126+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
127+
labs(title = "Distribution of Canadian towns by English and French fluency",
128+
x = "% fluent in English",
129+
y = "% fluent in French",
130+
fill = "population") +
131+
theme_bw() +
132+
theme(text = element_text(family = 'Fira Sans'))
133+
p <- ggplotly(p)
134+
135+
136+
# Create a shareable link to your chart
137+
# Set up API credentials: https://plot.ly/r/getting-started
138+
chart_link = api_create(p, filename="geom_hex/customize-theme")
139+
chart_link
140+
```
141+
142+
```{r echo=FALSE}
143+
chart_link
144+
```
145+

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

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
46+
```r
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+
<iframe src="https://plot.ly/~RPlotBot/5729.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
66+
67+
### Customized Colours
68+
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.
69+
70+
71+
```r
72+
library(plotly)
73+
74+
p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
75+
geom_hex() +
76+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
77+
labs(title = "Distribution of Canadian towns by English and French fluency",
78+
x = "% fluent in English",
79+
y = "% fluent in French",
80+
fill = "# of census \nsubdivisions")
81+
p <- ggplotly(p)
82+
83+
# Create a shareable link to your chart
84+
# Set up API credentials: https://plot.ly/r/getting-started
85+
chart_link = api_create(p, filename="geom_hex/log-chart")
86+
chart_link
87+
```
88+
89+
<iframe src="https://plot.ly/~RPlotBot/5731.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
90+
91+
### Weighted Data
92+
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.
93+
94+
95+
```r
96+
library(plotly)
97+
98+
p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
99+
geom_hex() +
100+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
101+
labs(title = "Distribution of the Canadian population by English and French fluency",
102+
x = "% fluent in English",
103+
y = "% fluent in French",
104+
fill = "population")
105+
ggplotly(p)
106+
107+
# Create a shareable link to your chart
108+
# Set up API credentials: https://plot.ly/r/getting-started
109+
chart_link = api_create(p, filename="geom_hex/weighted-data")
110+
chart_link
111+
```
112+
113+
<iframe src="https://plot.ly/~RPlotBot/5733.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
114+
115+
### Customized Appearance
116+
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.
117+
118+
119+
```r
120+
library(plotly)
121+
122+
p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
123+
geom_hex(bins = 20) +
124+
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
125+
labs(title = "Distribution of Canadian towns by English and French fluency",
126+
x = "% fluent in English",
127+
y = "% fluent in French",
128+
fill = "population") +
129+
theme_bw() +
130+
theme(text = element_text(family = 'Fira Sans'))
131+
p <- ggplotly(p)
132+
133+
134+
# Create a shareable link to your chart
135+
# Set up API credentials: https://plot.ly/r/getting-started
136+
chart_link = api_create(p, filename="geom_hex/customize-theme")
137+
chart_link
138+
```
139+
140+
<iframe src="https://plot.ly/~RPlotBot/5739.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>
141+

0 commit comments

Comments
 (0)