geom_hex


Divides the plot plane into regular hexagons and counts the number of cases in each hexagon. Result (by default count) is mapped to fill aesthetic. This is a useful alternative to geom_point in the presence of overplotting.

Aesthetics

x, y required position aesthetics
alpha, colour, fill, group, size classic aesthetics properties

Other Properties

bins numeric vector giving number of bins in both vertical and horizontal directions. Set to 30 by default
binwidth numeric vector giving bin width in both vertical and horizontal directions. Overrides bins if both set

Computed Variables

count number of points in bin
density density estimate
ncount count, scaled to maximum of 1
ndensity density estimate, scaled to maximum of 1

Similar Geometries

geom_bin2d, geom_density2D

Description and Details

Using the described geometry, you can insert a geometric layer into your data visualization – a hexagons defined by positional aesthetics properties (x and y) and count as Computed Variable. You can find this geometry in the ribbon toolbar tab Layers, under the 2D button.

As with geom_bin2d geometry, you can use geom_hex to display continuous bivariate distribution. Use this geometry as an alternative if you want to display the spatial distribution of a large number of values (points). As an example, we can use a diamonds dataset that contains over 60,000 records. An example of the relationship between the price of diamond and carat is shown in the following figure.

There is a clear intensive point’s overplotting. If you want to display the number of values in the plot area, you can also use geom_hex geometry layer. In this case (just as for point geometry) you define two positional aesthetic properties – x and y. Program automatically divide the area into individual hexagons and calculate the occurrence of cases in these hexagons. Subsequently, this number (count) is displayed in the aesthetic property – fill.

By default, the program creates 30 hexagons. This number is often not sufficient and we need to change it. For this we use the bins property. In the following figure, we've reduced the number of hexagons to 20.

You can also use the binwidth property to set the hexagon size, which defines the hexagon size in both directions (horizontally and vertically). Size is defined by the R function c(). In the following figure, the hexagon size was defined to 0.3 in the horizontal and 500 in the vertical direction.

For display, you can use default color palette or you can specify your own, using objects from the scale_fill group. An example of custom-defined color palette is shown in the following figure. In this case, we define the color scale using the rainbow palette and these colors were applied to values (count) higher than 1. The hexagons with value of 1 are displayed by default in a gray color (na.value).

In the same way, it is possible to define geom_bin2d geometry layer, which displays counts in rectangles. Alternatively, you can use geom_density_2d for the same purpose (spatial distribution).