ggplot2 geom_bin2d() in R: 2D Rectangular Density Bins
The geom_bin2d() function in ggplot2 plots 2D point density using RECTANGULAR bins. It is the rectangle-bin sister of geom_hex(), useful for scatter plots with many overlapping points.
ggplot(df, aes(x, y)) + geom_bin2d() geom_bin2d(bins = 30) # control bin count geom_bin2d(binwidth = c(0.5, 1)) # specific bin widths geom_hex() # hexagonal alternative geom_density_2d() # contour lines
Need explanation? Read on for examples and pitfalls.
What geom_bin2d() does in one sentence
geom_bin2d() divides the (x, y) plane into rectangular bins and colors each by point count. Equivalent to a 2D histogram.
Syntax
geom_bin2d(bins = 30, binwidth = NULL, ...). Built into ggplot2; no extra packages.
geom_bin2d doesn't need any extra package; geom_hex requires hexbin. Use bin2d if you want zero dependencies.Five common patterns
1. Standard 2D histogram
2. Finer bins
3. Custom binwidth
4. Color scale
5. Compare to geom_hex
geom_bin2d and geom_hex produce similar visualizations. Hex looks more organic; bin2d is simpler. Pick by aesthetic preference.geom_bin2d() vs geom_hex() vs geom_point() with alpha
| Function | Bin | Dependency |
|---|---|---|
geom_bin2d() |
Rectangular | None |
geom_hex() |
Hexagonal | hexbin |
geom_point(alpha) |
None | None |
For dependency-free 2D density, geom_bin2d is the choice.
A practical workflow
Use geom_bin2d for dense scatter where rectangular bins suffice.
Common pitfalls
Pitfall 1: too few bins. Default 30 is often coarse. Try 50 or 100.
Pitfall 2: forgetting fill scale. Default fill is gradient blue. Use scale_fill_viridis_c() or similar for better visibility.
geom_bin2d colors bins by COUNT. For density (count per unit area), use stat_density_2d or normalize.Try it yourself
Try it: Plot 2D bin histogram of mtcars wt vs mpg. Save to ex_plot.
Click to reveal solution
Explanation: mtcars has 32 points; few bins is appropriate.
Related ggplot2 functions
After mastering geom_bin2d, look at:
geom_hex(): hexagonal binsgeom_density_2d(): contour linesstat_density_2d(): smooth densitygeom_point(alpha): scatter alternative
FAQ
What does geom_bin2d do in ggplot2?
geom_bin2d() plots 2D point density using rectangular bins; each bin is colored by point count.
What is the difference between geom_bin2d and geom_hex?
geom_bin2d uses rectangles; geom_hex uses hexagons. Hex is more organic-looking; bin2d is dependency-free.
Do I need a special package for geom_bin2d?
No. It is built into ggplot2.
How many bins should I use?
Default 30 is OK for medium data. For dense plots, try 50-100.
How do I show density (not count) per bin?
Use stat_density_2d for smoothed density, or normalize the count manually before plotting.