geom_oblicubes: Draw 2D/3D cubes with ggplot2

View source: R/geom_oblicubes.R

geom_oblicubesR Documentation

Draw 2D/3D cubes with ggplot2

Description

geom_oblicubes() creates a ggplot2 geom that draws cubes.

Usage

geom_oblicubes(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  angle = 45,
  scale = 0.5,
  xoffset = 0,
  yoffset = 0,
  zoffset = 0,
  light = darken_face,
  show.legend = NA,
  inherit.aes = TRUE
)

Arguments

mapping

Set of aesthetic mappings created by aes() or aes_(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (e.g. ~ head(.x, 10)).

stat

The statistical transformation to use on the data for this layer, as a string.

position

Position adjustment, either as a string, or the result of a call to a position adjustment function.

...

Aesthetics, used to set an aesthetic to a fixed value.

angle

Oblique projection angle.

scale

Oblique projection foreshortening factor. 0.5 corresponds to the “cabinet projection”. 1.0 corresponds to the “cavalier projection”. 0.0 corresponds to a “primary view orthographic projection”.

xoffset, yoffset, zoffset

By default the x,y,z values are assumed to be the center of the cube. Use xoffset, yoffset, and/or zoffset to shift the x,y,z values a fixed amount.

light

If FALSE don't perform a "light" effect. Otherwise a function that takes two arguments: the first face of the cube/cuboid face (one of "top", "west", "east", "south", "north"). the second col of the fill color. By default we use darken_face().

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().

Details

geom_oblicubes() requires a fixed scale coordinate system with an aspect ratio of 1 as provided by ggplot2::coord_fixed().

Value

A ggplot2 geom.

Aesthetics

geom_oblicubes() understands the following aesthetics (required aesthetics are in bold). See oblicubesGrob() for more details.

  • x

  • y

  • z

  • fill

  • colour

  • linetype

  • linewidth

See Also

geom_oblicubes() is a wrapper around oblicubesGrob().

Examples

if (require("ggplot2")) {
  data("volcano", package = "datasets")
  df <- xyz_heightmap(volcano, scale = 0.3, min = 1)
  g <- ggplot(df, aes(x, y, z = z, fill = raw)) +
         geom_oblicubes(light = FALSE) +
         coord_fixed() +
         scale_fill_gradientn(name = "Height (m)",
                              colours=terrain.colors(256)) +
         labs(x = "East (10m)", y = "North (10m)",
              title = "Maungawhau (`datasets::volcano`)")
  plot(g)
}

if (require("ggplot2")) {
  # Using `scale_fill_identity()` if using `xyz_heightmap()`'s `fill` column
  df <- xyz_heightmap(volcano, scale = 0.3, min = 1,
                      col = grDevices::heat.colors)
  g <- ggplot(df, aes(x, y, z = z, fill = fill)) +
         geom_oblicubes() +
         coord_fixed() +
         scale_fill_identity()
  plot(g)
}

if (require("ggplot2") && require("dplyr")) {
  # Note you probably should not do 3D bar charts...
  df <- as.data.frame(datasets::Titanic) |>
          filter(Age == "Child", Freq > 0) |>
          group_by(Sex, Survived, Class) |>
          summarize(Freq = seq.int(sum(Freq)), .groups = "drop")
  g <- ggplot(df, aes(x = Survived, y = Freq, fill = Survived)) +
      facet_grid(cols = vars(Class, Sex)) +
      coord_fixed() +
      geom_oblicubes(yoffset = -0.5, zoffset = -0.5, angle = -45, scale = 0.7) +
      scale_fill_manual(values = c("Yes" = "lightblue", "No" = "red")) +
      scale_y_continuous(expand = expansion(), name = "") +
      scale_x_discrete(name = "", breaks = NULL) +
      labs(title = "Children on the Titanic (by ticket class)")
  plot(g)
}

oblicubes documentation built on Aug. 27, 2022, 5:05 p.m.