Add a standalone z-axis to the active 3D scene.

Cache fallback messages are disabled by default. Set options(rayshader.verbose_scene_cache = TRUE) to print when cached metadata is reused.

render_zaxis(
  data = "auto",
  location = "auto",
  breaks = NULL,
  labels = NULL,
  title = "auto",
  title_location = "side",
  title_offset = 1.25,
  title_size = NULL,
  color = "black",
  linewidth = 2,
  text_offset = 0,
  label_size = 0.8,
  label_side = "auto",
  title_side = "auto",
  corner_offset = NULL,
  tick_size = NULL,
  extent = NULL,
  panel = NULL,
  zscale = 1,
  vertical_exaggeration = 1,
  heightmap = NULL
)

Arguments

data

Default "auto". Data source used to generate z-axis breaks and labels. Options are "auto", "topographic", "polygon", "point", "path", "obj", "raymesh", "tree", "label", "building", and "cloud". Use "polygon" after render_polygons() or render_beveled_polygons() to label breaks with cached polygon data values (for example data_column_top) while placing ticks at the rendered scaled heights. Use "building" after render_buildings() to refer to building height data, or the matching render type after point/path/object/tree/label and cloud calls to use those cached altitude values.

location

Default "auto". Axis location. Options: "auto", "panel", "panelbottomleft", "panelbottomright", "paneltopleft", "paneltopright", "bottomleft", "bottomright", "topleft", "topright".

breaks

Default NULL. Numeric breaks (in altitude units). If NULL, breaks are generated from the full scene height range (minimum to maximum elevation).

labels

Default NULL. Labels for breaks.

title

Default "auto". Title for the z-axis. If "auto", rayshader uses the cached height aesthetic label from plot_gg() scenes when available. Set to NULL to omit the title, or pass a character string to override it.

title_location

Default "side". Title location. Options are "side" and "top". "side" places the title horizontally opposite the tick-label side by default and centered on the axis.

title_offset

Default 1.25. Title offset multiplier in tick lengths. For title_location = "side", this moves the title away from the axis on the chosen title side, but never closer than one tick length beyond the tick label offset. For "top", this moves the title above the top of the axis using tick-length-scaled spacing.

title_size

Default NULL. Matches label_size when omitted. Title text size passed to rgl::texts3d() as cex.

color

Default "black". Axis/tick/label color.

linewidth

Default 2. Axis line width.

text_offset

Default 0. Label offset multiplier from the axis, applied in the outward corner direction (diagonal for corner placements).

label_size

Default 0.8. Tick label text size passed to rgl::texts3d() as cex.

label_side

Default "auto". Side of the axis where tick labels are placed from the current camera perspective. Options are "auto", "left", and "right". "auto" keeps the inferred outside-corner side.

title_side

Default "auto". Side of the axis where the title is placed from the current camera perspective. Options are "auto", "left", and "right". "auto" places side titles opposite the tick-label side and top titles on the tick-label side.

corner_offset

Default NULL. Corner offset as a proportion of the center-to-corner planar distance. If NULL, this defaults to 0 for ggplot scenes and 0.08 for non-ggplot scenes. 0 places the axis exactly at the corner.

tick_size

Default NULL. Tick marker size. If NULL, auto-sized from line width.

extent

Default NULL. Either an object representing the spatial extent of the scene (either from the raster, terra, sf, or sp packages), a length-4 numeric vector specifying c("xmin", "xmax","ymin","ymax"), or the spatial object (from the previously aforementioned packages) which will be automatically converted to an extent object. If omitted, rayshader will use cached extent metadata from plot_gg() or from plot_3d() (either from an explicitly passed extent argument, or the built-in montereybay_spatial scene metadata).

panel

Default NULL. Facet panel identifier for scenes created with plot_gg(). Required to disambiguate faceted ggplot scenes when panel-specific cached metadata is needed. Ignored for non-ggplot scenes.

zscale

Default 1. The ratio between x/y spacing and z units. If left at 1 with breaks = NULL on non-ggplot terrain scenes, rayshader will attempt to use the cached plot_3d() zscale to generate more meaningful defaults.

vertical_exaggeration

Default 1. Multiplier applied to the effective visual relief. If omitted, rayshader uses the cached scene value from plot_3d() or plot_gg() when available; pass explicitly to override for this call.

heightmap

Default NULL. Height matrix for the current scene. If omitted, this is taken from the cached scene set by plot_3d() or plot_gg(). Pass explicitly to override the cached value.

Examples

# Add a z-axis to a Monterey Bay terrain scene. The spatial extent and zscale
# are cached by sphere_shade()/plot_3d(), so render_zaxis() can infer them.
montereybay_spatial |>
 sphere_shade(texture = "imhof1", vertical_exaggeration = 20) |>
 plot_3d(
   vertical_exaggeration = 4,
   water = TRUE,
   theta = 245,
   phi = 20,
   zoom = 1
 )
render_zaxis(
 location = "bottomleft",
 color = "black",
 corner_offset = 0.2
)
render_snapshot()


#Change the location
render_zaxis(
 location = "topright",
 title = "Elevation (m)",
 title_location = "top",
 color = "red",
 corner_offset = 0.3,
 tick_size = 3
)
render_snapshot()

# Add a z-axis to a ggplot scene.
library(ggplot2)
mtplot = ggplot(mtcars) +
 geom_point(aes(x = mpg, y = disp, color = cyl)) +
 scale_color_continuous(limits = c(0, 8))
plot_gg(
 mtplot,
 width = 3.5,
 window_size = c(1400, 866),
 sun_angle = 225,
 zoom = 0.50,
 phi = 20,
 theta = 45
)
render_zaxis(location = "panel_bottomleft", color = "red",
            label_size = 1.5)
render_snapshot()


# For faceted ggplot scenes, specify the panel whose corner should anchor the axis.
density_plot = ggplot(mtcars) +
 stat_density_2d(
   aes(x = mpg, y = disp, fill = after_stat(density)),
   geom = "raster",
   contour = FALSE
 ) +
 facet_wrap(~cyl) +
 scale_x_continuous(expand = c(0, 0)) +
 scale_y_continuous(expand = c(0, 0)) +
 scale_fill_gradient(low = "pink", high = "red")
plot_gg(
 density_plot,
 width = 6,
 window_size = c(1400, 866),
 zoom = 0.55,
 theta = 45,
 phi = 25,
 vertical_exaggeration = 300
)
render_zaxis(
 panel = 1,
 location = "panel_bottomleft"
)
render_snapshot()

#Move to the second panel, top left
render_camera(theta=-20, phi=20)
render_zaxis(
 panel = 2,
 location = "panel_topleft"
)
render_snapshot()

#Move to corner of the plot
render_camera(theta=-20, phi=20,zoom=0.6)
render_zaxis(
 location = "topleft"
)
render_snapshot()