|
| 1 | +--- |
| 2 | +title: geom_bin2d | Examples | Plotly |
| 3 | +name: geom_bin2d |
| 4 | +permalink: ggplot2/geom_bin2d/ |
| 5 | +description: How to make a 2-dimensional heatmap in ggplot2 using geom_bin2d. Examples of coloured and facetted graphs. |
| 6 | +layout: base |
| 7 | +thumbnail: thumbnail/geom_bin2d.jpg |
| 8 | +language: ggplot2 |
| 9 | +page_type: example_index |
| 10 | +has_thumbnail: true |
| 11 | +display_as: statistical |
| 12 | +order: 2 |
| 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 | + |
| 43 | +```{r, results='hide'} |
| 44 | +library(plotly) |
| 45 | +
|
| 46 | +english_french <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/english_french.csv",stringsAsFactors = FALSE) |
| 47 | +
|
| 48 | +p <- ggplot(english_french, aes(x=engperc,y=frenperc)) + |
| 49 | + geom_bin2d() + |
| 50 | + labs(title = "Distribution of Canadian areas by English and French fluency", |
| 51 | + x = "% fluent in English", |
| 52 | + y = "% fluent in French", |
| 53 | + fill = "# of census \nsubdivisions") |
| 54 | +p <- ggplotly(p) |
| 55 | +
|
| 56 | +# Create a shareable link to your chart |
| 57 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 58 | +chart_link = api_create(p, filename="geom_bin2d/2d-chart") |
| 59 | +chart_link |
| 60 | +``` |
| 61 | + |
| 62 | +```{r echo=FALSE} |
| 63 | +chart_link |
| 64 | +``` |
| 65 | + |
| 66 | +### Customized Colours |
| 67 | +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. |
| 68 | + |
| 69 | +```{r, results='hide'} |
| 70 | +library(plotly) |
| 71 | +
|
| 72 | +p <- ggplot(english_french, aes(x=engperc,y=frenperc)) + |
| 73 | + geom_bin2d() + |
| 74 | + scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") + |
| 75 | + labs(title = "Distribution of Canadian towns by English and French fluency", |
| 76 | + x = "% fluent in English", |
| 77 | + y = "% fluent in French", |
| 78 | + fill = "# of census \nsubdivisions") |
| 79 | +p <- ggplotly(p) |
| 80 | +
|
| 81 | +# Create a shareable link to your chart |
| 82 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 83 | +chart_link = api_create(p, filename="geom_bin2d/log-chart") |
| 84 | +chart_link |
| 85 | +``` |
| 86 | + |
| 87 | +```{r echo=FALSE} |
| 88 | +chart_link |
| 89 | +``` |
| 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 | +```{r, results='hide'} |
| 95 | +library(plotly) |
| 96 | +
|
| 97 | +p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) + |
| 98 | + geom_bin2d() + |
| 99 | + scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") + |
| 100 | + labs(title = "Distribution of the Canadian population by English and French fluency", |
| 101 | + x = "% fluent in English", |
| 102 | + y = "% fluent in French", |
| 103 | + fill = "# of people") |
| 104 | +p <- ggplotly(p) |
| 105 | +
|
| 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_bin2d/weighted-data") |
| 110 | +chart_link |
| 111 | +``` |
| 112 | + |
| 113 | +```{r echo=FALSE} |
| 114 | +chart_link |
| 115 | +``` |
| 116 | + |
| 117 | +### With Facets |
| 118 | +We can facet the graphic with the "region" column, and set "bins" to 20, so that the graph is 20 x 20 sides. |
| 119 | + |
| 120 | +```{r, results='hide'} |
| 121 | +library(plotly) |
| 122 | +
|
| 123 | +p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) + |
| 124 | + geom_bin2d(bins = 20) + |
| 125 | + facet_wrap(~factor(region, levels = c("Atlantic","Québec","Ontario","Prairies","British Columbia"))) + |
| 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 = "# of people") |
| 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_bin2d/with-facet") |
| 137 | +chart_link |
| 138 | +``` |
| 139 | + |
| 140 | +```{r echo=FALSE} |
| 141 | +chart_link |
| 142 | +``` |
| 143 | + |
| 144 | +### Customized Appearance |
| 145 | +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. |
| 146 | + |
| 147 | +```{r, results='hide'} |
| 148 | +library(plotly) |
| 149 | +
|
| 150 | +p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) + |
| 151 | + geom_bin2d(bins = 20) + |
| 152 | + facet_wrap(~factor(region, levels = c("Atlantic","Québec","Ontario","Prairies","British Columbia"))) + |
| 153 | + scale_fill_gradient(low="lightblue1",high="darkblue",trans="log10") + |
| 154 | + labs(title = "Distribution of Canadian towns by English and French fluency", |
| 155 | + x = "% fluent in English", |
| 156 | + y = "% fluent in French", |
| 157 | + fill = "# of people") + |
| 158 | + theme_bw() + |
| 159 | + theme(text = element_text(family = 'Fira Sans')) |
| 160 | +p <- ggplotly(p) |
| 161 | +
|
| 162 | +
|
| 163 | +# Create a shareable link to your chart |
| 164 | +# Set up API credentials: https://plot.ly/r/getting-started |
| 165 | +chart_link = api_create(p, filename="geom_bin2d/customize-theme") |
| 166 | +chart_link |
| 167 | +``` |
| 168 | + |
| 169 | +```{r echo=FALSE} |
| 170 | +chart_link |
| 171 | +``` |
| 172 | + |
0 commit comments