|
| 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