ggplot2 geom_hex() in R: 2D Hexagonal Density Bins
The geom_hex() function in ggplot2 plots 2D point density using HEXAGONAL bins. It is ideal for scatter plots with many overlapping points, providing a clearer view than transparent geom_point.
ggplot(df, aes(x, y)) + geom_hex() geom_hex(bins = 30) # control bin count geom_hex() + scale_fill_viridis_c() geom_bin2d() # rectangular bins alternative geom_density_2d() # contour lines
Need explanation? Read on for examples and pitfalls.
What geom_hex() does in one sentence
geom_hex() divides the (x, y) plane into hexagonal bins and colors each by the count of points falling in it. Best for dense scatter data where overlapping obscures patterns.
Syntax
geom_hex(bins = 30, binwidth = NULL, ...). Requires hexbin package.
hexbin package: install.packages("hexbin"). geom_hex requires it for the binning math.Five common patterns
1. Standard hex density
2. Custom bin count
3. Custom binwidth
4. Color scale
5. With overlay
geom_hex is much clearer than geom_point(alpha=0.3). Hex bins highlight density patterns that transparent points hide.geom_hex() vs geom_bin2d() vs geom_point() with alpha
| Function | Bin shape | Best for |
|---|---|---|
geom_hex() |
Hexagonal | Dense scatter with smooth visual |
geom_bin2d() |
Rectangular | Dense scatter, rectangle-friendly |
geom_point(alpha = 0.3) |
None (raw points) | Fewer points |
geom_density_2d() |
Contour lines | Smooth density |
A practical workflow
Use geom_hex when scatter density is the question.
Density on log-y for skewed price data.
Common pitfalls
Pitfall 1: forgetting hexbin package. geom_hex requires hexbin. Install if not already.
Pitfall 2: too few bins. Default 30 bins may be too coarse; try 50 or 100 for fine detail.
geom_hex() requires the hexbin package. Without it, the function errors. Always test imports.Try it yourself
Try it: Plot a hex density of mpg vs hp from mtcars. Save to ex_plot.
Click to reveal solution
Explanation: mtcars has only 32 points so use fewer bins. Hex shows density even with sparse data.
Related ggplot2 functions
After mastering geom_hex, look at:
geom_bin2d(): rectangular bins alternativegeom_density_2d(): contour linesgeom_point(alpha): raw scatterstat_density_2d(): density estimation
FAQ
What does geom_hex do in ggplot2?
geom_hex() plots 2D point density using hexagonal bins. Each bin is colored by the count of points within it.
Do I need a special package for geom_hex?
Yes. Install hexbin: install.packages("hexbin"). Without it, geom_hex errors.
What is the difference between geom_hex and geom_bin2d?
geom_hex uses hexagonal bins; geom_bin2d uses rectangular. Hexagons reduce visual artifacts at bin boundaries; rectangles are simpler.
How many bins should I use?
Default 30 is OK for medium data. For >10k points, try 50-100 for finer detail.
Should I use geom_hex or scatter with alpha?
For 1k+ overlapping points, geom_hex is clearer. For <1k, geom_point with alpha works well.