ggcube draws 3D geometry with the painter's algorithm: elements are drawn back to front, so that nearer elements cover farther ones. All ggcube layers in a panel are sorted together, as one scene, so the geometry of one layer can pass in front of or behind that of another. (Layers from other packages, and the layers on either side of them, are sorted separately.)
Details
Sorting compares every pair of elements that overlap on screen, including polygons, points, segments, and text. Polygons that pass through one another are first split along their line of intersection, and segments that pass through polygons are split where they cross them. Wherever a polygon and another element overlap, their order is determined exactly from their 3D geometry where possible, and otherwise estimated from depths sampled in the region of overlap. Point markers and lines are compared at their drawn size, so overlapping markers are drawn farther first. Points, segments, and text lying in the plane of a planar polygon are drawn on top of it, and coplanar polygons from different layers are drawn in layer order. Results are exact for planar polygons; non-planar polygons (e.g. tiles of a curved surface on a rectangular grid) are treated as approximately planar.
Elements are ordered when the plot is drawn, since the drawn size of markers and lines depends on the size of the panel.
Earlier versions sorted each layer separately, and offered a choice of
sorting algorithms through layers' sort_method argument, which is now
deprecated and has no effect.
