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(). If specified andinherit.aes = TRUE(the default), it is combined with the default mapping at the top level of the plot. You must supplymappingif 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 toggplot().A
data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. Seefortify()for which variables will be created.A
functionwill be called with a single argument, the plot data. The return value must be adata.frame, and will be used as the layer data. Afunctioncan be created from aformula(e.g.~ head(.x, 10)).- stat
The statistical transformation to use on the data for this layer. When using a
geom_*()function to construct a layer, thestatargument can be used the override the default coupling between geoms and stats. Thestatargument accepts the following:A
Statggproto subclass, for exampleStatCount.A string naming the stat. To give the stat as a string, strip the function name of the
stat_prefix. For example, to usestat_count(), give the stat as"count".For more information and other ways to specify the stat, see the layer stat documentation.
- position
A position adjustment to use on the data for this layer. This can be used in various ways, including to prevent overplotting and improving the display. The
positionargument accepts the following:The result of calling a position function, such as
position_jitter(). This method allows for passing extra arguments to the position.A string naming the position adjustment. To give the position as a string, strip the function name of the
position_prefix. For example, to useposition_jitter(), give the position as"jitter".For more information and other ways to specify the position, see the layer position documentation.
- ...
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/orzoffsetto shift the x,y,z values a fixed amount.- light
If
FALSEdon't perform a "light" effect. Otherwise a function that takes two arguments: the firstfaceof the cube/cuboid face (one of "top", "west", "east", "south", "north"). the secondcolof the fill color. By default we usedarken_face().- show.legend
logical. Should this layer be included in the legends?
NA, the default, includes if any aesthetics are mapped.FALSEnever includes, andTRUEalways 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().
Aesthetics
geom_oblicubes() understands the following aesthetics (required aesthetics are in bold).
See oblicubesGrob() for more details.
xyzfillcolourlinetypelinewidth
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)
}
#> Loading required package: ggplot2
# \donttest{
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)
}
#> Loading required package: dplyr
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
#> Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
#> dplyr 1.1.0.
#> ℹ Please use `reframe()` instead.
#> ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
#> always returns an ungrouped data frame and adjust accordingly.