In this example, it looks like the ones place is mapped to one color channel (maxing out at 10), ditto for tens and hundreds. Each channel is mapped to a geom_hex
layer, with the hexagons scaled by size.
I don't think this is currently possible "out of the box" with ggplot, since the geom_hex
geom does not allow mapping to size:
Github issue closed in 2018, deciding to add mapping to size for
geom_hex, but only for line thickness, not hex size itself:
https://github.com/tidyverse/ggplot2/issues/2488
Workaround outside ggplot:
https://stackoverflow.com/a/16960960/6851825
That said, we can do something similar with geom_point. It would take some more fiddling to create a custom legend like the example, but should be possible.
# Count within each hex area:
example.hex <- hexbin::hexbin(x1, x2,
xbins = 30, IDs = TRUE)
value_by_hexbin <- data.frame(hexbin::hcell2xy(example.hex),
cell = example.hex@cell,
count = example.hex@count)
# Plot those counts to three layers of point, for each color channel
ggplot(value_by_hexbin,
aes(x, y)) +
geom_point(aes(size = pmin(10, floor(count / 1))), color = "purple") +
geom_point(aes(size = pmin(10, floor(count / 10))), color = "green") +
geom_point(aes(size = pmin(10, floor(count / 100))), color = "red") +
scale_size_area(max_size = 3) +
coord_equal()
