New to Plotly?
Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!
##  '4.9.1'
Basic 2D Histogram
2D histograms require
y, but in contrast to heatmaps,
z is optional. If
z is not provided, binning occurs in the browser (see here for a list of binning options).
# install.packages('mvtnorm') library(plotly) s <- matrix(c(1, -.75, -.75, 1), ncol = 2) obs <- mvtnorm::rmvnorm(500, sigma = s) p <- plot_ly(x = obs[,1], y = obs[,2]) pp <- subplot( p %>% add_markers(alpha = 0.2), p %>% add_histogram2d() ) pp
z is not provided, the only way to control coloring is through the colorscale attribute
p <- p %>% add_histogram2d(colorscale = "Blues") p
If you want more control for the binning algorithm, you can supply a 2D table or matrix to
z. In this case, the R package will impose it's colorscale default (and the
colors argument can be used to control the colorscale from R):
cnt <- with(diamonds, table(cut, clarity)) p <- plot_ly(diamonds, x = ~cut, y = ~clarity, z = ~cnt) %>% add_histogram2d() p