Skip to content

added geom_hex #1434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ vendor/bundle
_posts/python/fundamentals/static-image/images
_config_personal.yml
_posts/python/html
.Rproj.user
documentation.Rproj
9 changes: 6 additions & 3 deletions _posts/ggplot2/2019-07-12-geom_bin2d.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ packageVersion('plotly')
```

### Basic 2d Heatmap
See also geom\_hex for a similar geom with hexagonal bins. Note: facetting is supported in geom\_bin2d but not geom\_hex.

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

```{r, results='hide'}
library(plotly)
Expand Down Expand Up @@ -100,7 +103,7 @@ p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
labs(title = "Distribution of the Canadian population by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "# of people")
fill = "population")
p <- ggplotly(p)


Expand All @@ -127,7 +130,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
labs(title = "Distribution of Canadian towns by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "# of people")
fill = "population")
p <- ggplotly(p)


Expand All @@ -154,7 +157,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
labs(title = "Distribution of Canadian towns by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "# of people") +
fill = "population") +
theme_bw() +
theme(text = element_text(family = 'Fira Sans'))
p <- ggplotly(p)
Expand Down
12 changes: 7 additions & 5 deletions _posts/ggplot2/2019-07-12-geom_bin2d.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ packageVersion('plotly')
```

```
## [1] '4.9.0'
## [1] '4.9.0.9000'
```

### Basic 2d Heatmap
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.

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

```r
library(plotly)
Expand Down Expand Up @@ -100,7 +102,7 @@ p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
labs(title = "Distribution of the Canadian population by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "# of people")
fill = "population")
p <- ggplotly(p)


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

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


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


Expand All @@ -152,7 +154,7 @@ p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
labs(title = "Distribution of Canadian towns by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "# of people") +
fill = "population") +
theme_bw() +
theme(text = element_text(family = 'Fira Sans'))
p <- ggplotly(p)
Expand Down
153 changes: 153 additions & 0 deletions _posts/ggplot2/2019-07-30-geom_hex.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
---
title: geom_hex | Examples | Plotly
name: geom_hex
permalink: ggplot2/geom_hex/
description: How to make a hexagonal two-dimensional heatmap in ggplot2 using geom_hex. Examples of coloured and facetted graphs.
layout: base
thumbnail: thumbnail/geom_hex.jpg
language: ggplot2
page_type: example_index
has_thumbnail: true
display_as: statistical
order: 3
output:
html_document:
keep_md: true
---

```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
Sys.setenv("plotly_username"="RPlotBot")
Sys.setenv("plotly_api_key"="q0lz6r5efr")
```

### New to Plotly?

Plotly's R library is free and open source!<br>
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
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>
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!

### Version Check

Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
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.

```{r}
library(plotly)
packageVersion('plotly')
```

### Basic 2d Heatmap
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.

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

```{r, results='hide'}
library(plotly)

english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)

p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
geom_hex() +
labs(title = "Distribution of Canadian areas by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "# of census \nsubdivisions")
p <- ggplotly(p)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="geom_hex/2d-chart")
chart_link
```

```{r echo=FALSE}
chart_link
```

### Customized Colours
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.

```{r, results='hide'}
library(plotly)

english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)

p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if english_french was assigned in this example too so that the code here could run independently of the first one.

geom_hex() +
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
labs(title = "Distribution of Canadian towns by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "# of census \nsubdivisions")
p <- ggplotly(p)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="geom_hex/log-chart")
chart_link
```

```{r echo=FALSE}
chart_link
```

### Weighted Data
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.

```{r, results='hide'}
library(plotly)

english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)

p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above re: english_french

geom_hex() +
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
labs(title = "Distribution of the Canadian population by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "population")
ggplotly(p)


# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="geom_hex/weighted-data")
chart_link
```

```{r echo=FALSE}
chart_link
```

### Customized Appearance
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.

```{r, results='hide'}
library(plotly)

english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)

p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, if we could assign english_french in this example before using it, that would be great.

geom_hex(bins = 20) +
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
labs(title = "Distribution of Canadian towns by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "population") +
theme_bw() +
theme(text = element_text(family = 'Fira Sans'))
p <- ggplotly(p)


# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="geom_hex/customize-theme")
chart_link
```

```{r echo=FALSE}
chart_link
```

148 changes: 148 additions & 0 deletions _posts/ggplot2/2019-07-30-geom_hex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---
title: geom_hex | Examples | Plotly
name: geom_hex
permalink: ggplot2/geom_hex/
description: How to make a hexagonal two-dimensional heatmap in ggplot2 using geom_hex. Examples of coloured and facetted graphs.
layout: base
thumbnail: thumbnail/geom_hex.jpg
language: ggplot2
page_type: example_index
has_thumbnail: true
display_as: statistical
order: 3
output:
html_document:
keep_md: true
---



### New to Plotly?

Plotly's R library is free and open source!<br>
[Get started](https://plot.ly/r/getting-started/) by downloading the client and [reading the primer](https://plot.ly/r/getting-started/).<br>
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>
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf) (new!) to help you get started!

### Version Check

Version 4 of Plotly's R package is now [available](https://plot.ly/r/getting-started/#installation)!<br>
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.


```r
library(plotly)
packageVersion('plotly')
```

```
## [1] '4.9.0.9000'
```

### Basic 2d Heatmap
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.

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

```r
library(plotly)

english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)

p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
geom_hex() +
labs(title = "Distribution of Canadian areas by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "# of census \nsubdivisions")
p <- ggplotly(p)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="geom_hex/2d-chart")
chart_link
```

<iframe src="https://plot.ly/~RPlotBot/5729.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

### Customized Colours
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.


```r
library(plotly)

english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)

p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
geom_hex() +
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
labs(title = "Distribution of Canadian towns by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "# of census \nsubdivisions")
p <- ggplotly(p)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="geom_hex/log-chart")
chart_link
```

<iframe src="https://plot.ly/~RPlotBot/5731.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

### Weighted Data
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.


```r
library(plotly)

english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)

p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
geom_hex() +
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
labs(title = "Distribution of the Canadian population by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "population")
ggplotly(p)

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="geom_hex/weighted-data")
chart_link
```

<iframe src="https://plot.ly/~RPlotBot/5733.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>

### Customized Appearance
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.


```r
library(plotly)

english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE)

p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
geom_hex(bins = 20) +
scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") +
labs(title = "Distribution of Canadian towns by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "population") +
theme_bw() +
theme(text = element_text(family = 'Fira Sans'))
p <- ggplotly(p)


# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="geom_hex/customize-theme")
chart_link
```

<iframe src="https://plot.ly/~RPlotBot/5739.embed" width="800" height="600" id="igraph" scrolling="no" seamless="seamless" frameBorder="0"> </iframe>