In fact, columnar jointing like this is common in nature and also appears in other rock types all around the world, especially in other lavas, subvolcanic intrusive (i.e. dikes, sills, laccoliths) igneous rocks, and welded tuffs/ignimbrites, but also in sandstones (in the case of sandstones, they are nearly always associated with heat transfer from nearby magmatic intrusions—note: please ignore the unscientific BS about “plasma” in the previous link—I linked it solely for the great photos of columnar sandstone, which are hard to come by).
In the case of columns in lavas and other igneous rocks, tensile stresses develop parallel to the cooling surface, and in the case of sandstones, they develop parallel to the heating surface. In the case of lavas, these stresses develop as the lava or magma slowly and progressively cools from the outside-inwards, because solid, cooled rock is denser than liquid and hot rock. Stresses develop in columnar sandstones because sandstones are porous, which allows the passage and presence of water within them, and this water is heated by underlying igneous intrusions causing hydrothermal circulation and convection to occur.
Many igneous intrusions and lava flows are tabular in shape, which results in regular, extensive colonnades, but many times you will also see columns that are oriented radially, for example, in a former volcanic conduit or neck, because the cooling surface was cylindrical, around the walls of the conduit, oriented horizontally, in the case of vertically emplaced dikes, or every-which-way, for example, when columns form in a lava flow that fills an irregularly-shaped valley, or in subglacial eruptions, where the lava has irregular ice-marginal contact. The important point to realize here is that the tensile stress is always parallel to the thermally-contrastive surface, and so joints develop normal to that surface.
These columnar joints begin forming from existing cracks on opposite surfaces of the rock layer. As the cracks develop, and they develop sequentially, bit by bit—the size of that bit determined by scaling laws that relate to the spacing of the joints and the strength of the rock (ultimately this forms secondary lines called striae across the columns, I won’t get into this, but check out the Phillips et al. and Goehring & Morris papers linked at the end for the detail)—and each time they crack, the direction of the crack changes slightly, guided by the tensile stress field that permeates the rock (again, from thermal energy). At the beginning of the process, these joints are immature, and the degree of hexagonality is very low—the columns may start out irregular, of many shapes and sizes, with curved sides even. But gradually, over a large region where these tensile stresses are acting equally on the rock (for example, a large sill or lava flow cooling at the same rate, or a tabular body of sandstone with uniform porosity over a sill), the joints develop and mature, tending towards the most energy-efficient configuration. This same process occurs when corn starch dries, by the way—you can watch the maturation of columns actually occurring in this video.
This happens to coincide with a geometric construction known as a Voronoi tessellation. Essentially, the center of each column becomes the center of a Voronoi polygon, which is defined by sides which are equidistant from each surrounding other center of every other polygon. And the most efficient distribution of Voronoi polygons is all hexagons, spaced equally from one another. Therefore, if all other factors (permeation by precipitation, mineral changes, groundwater, irregular surfaces, internal cavities and fractures, etc) were able to be ignored, if a basalt flow could be allowed to mature infinitely, it would always tend towards perfect hexagonal columns. Of course, in nature things are not perfect, and basalt flows don’t have infinite joint length to spare or infinite time to cool, so eventually the columns are left in whatever state of maturation they ended up in. Typically that looks like mostly 6-sided columns, but with plenty of 5- and 7-sided columns, and a few 4- and 8-sided columns. Watch a Voronoi tessellation mature in this video (white-to-yellow-to-orange indicate hexagons—the whiter they are, the more regular in shape, while orange and blue indicate non-hexagons)
As you can see in this image, which charts a Voronoi tessellation as it becomes more mature, the number of 6-sided polygons peaks, and the number of other sides decreases.
Most columns are five- or six-sided, but some are also four-sided, seven-sided, and eight-sided. Here are some statistics of the number of sides of columns from outcrops all over the world. I highlighted the number of sides and also an index χₙ, the index of hexagonality, a calculated measure of how hexagonal the columns in an outcrop are (see below for an explanation).
—
A note on the index of hexagonality, χₙ:
χₙ gives the deviation of all columns in an outcrop from all being 6-sided. It is calculated using the formula:
χₙ = √(f₅ + f₇) + 4(f₄ + f₈) + 9(f₃ + f₉) + 16f₁₀ + 25f₁₁ + …
where fₙ is the fraction of columns in the outcrop with n sides
So basically, you add up the fraction of columns with 5 and 7 sides, plus 4 times the fraction of columns with 4 and 8 sides, plus 9 times the fraction of columns with 3 and 9 sides, and if any columns have more than 9 sides, you just keep multiplying that fraction by increasing squares, starting with 16 as the multiple—and then take the square root of that. This ultimately gives a value of χₙ that will be exactly 0 if all columns are hexagons, <1 if all columns were 5-,6-, or 7-sided, exactly 1 if all columns were 5- and 7-sided, >1 and <2 if all columns were 4-, 5-, 6- 7-, and 8-sided, etc.
—
All figures in this answer are from:
Budkewitsch & Robin. (1994). Modelling the evolution of columnar joints. Journal of Volcanology and Geothermal Research, 59(3), 219–239. https://doi.org/10.1016/0377-0273(94)90092-2
But see also:
Phillips et al. (2013). The formation of columnar joints produced by cooling in basalt at Staffa, Scotland. Bulletin of Volcanology, 75(6), 715. https://doi.org/10.1007/s00445-013-0715-4
Goehring & Morris. (2008). Scaling of columnar joints in basalt. Journal of Geophysical Research, 113(B10), B10203. https://doi.org/10.1029/2007JB005018
Morris. (2014). Cracking the Giant’s Causeway with a Tabletop Experiment [Lecture Recording]. University of Toronto; YouTube. https://youtu.be/c3TpGtUZEjc