geom_bin2d in ggplot2
How to make a 2-dimensional heatmap in ggplot2 using geom_bin2d. Examples of coloured and facetted graphs.
New to Plotly?
Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.
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
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_bin2d() +
labs(title = "Distribution of Canadian areas by English and French fluency",
x = "% fluent in English",
y = "% fluent in French",
fill = "# of census \nsubdivisions")
fig <- ggplotly(p)
fig
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.
library(plotly)
p <- ggplot(english_french, aes(x=engperc,y=frenperc)) +
geom_bin2d() +
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")
fig <- ggplotly(p)
fig
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.
library(plotly)
p <- ggplot(english_french, aes(x=engperc, y=frenperc, weight=total)) +
geom_bin2d() +
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")
fig <- ggplotly(p)
fig
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.
library(plotly)
p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
geom_bin2d(bins = 20) +
facet_wrap(~factor(region, levels = c("Atlantic","Québec","Ontario","Prairies","British Columbia"))) +
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")
fig <- ggplotly(p)
fig
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.
library(plotly)
p <- ggplot(english_french, aes(x=engperc,y=frenperc, weight=total)) +
geom_bin2d(bins = 20) +
facet_wrap(~factor(region, levels = c("Atlantic","Québec","Ontario","Prairies","British Columbia"))) +
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'))
fig <- ggplotly(p)
fig
What About Dash?
Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash for R at https://dashr.plot.ly/installation.
Everywhere in this page that you see fig
, you can display the same figure in a Dash for R application by passing it to the figure
argument of the Graph
component from the built-in dashCoreComponents
package like this:
library(plotly)
fig <- plot_ly()
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... )
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
app <- Dash$new()
app$layout(
htmlDiv(
list(
dccGraph(figure=fig)
)
)
)
app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)