-
-
Notifications
You must be signed in to change notification settings - Fork 544
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
added geom_hex #1434
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f99a62c
Rproj
kojisposts 112d54c
fix merge weirdness
kojisposts 0c1679c
added Rproj
kojisposts 9d089fd
added geom_hex, fixed problems with geom_bin2d
kojisposts 430786d
(added data source)
kojisposts 784241b
edited
kojisposts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) + | ||
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)) + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment as above re: |
||
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)) + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, if we could assign |
||
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 | ||
``` | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.