geom_piece | R Documentation |
geom_piece()
creates a ggplot2
geom.
aes_piece()
takes a data frame and generates
an appropriate ggplot2::aes()
mapping.
geom_piece(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
envir = getOption("piecepackr.envir", piecepackr::game_systems()),
op_scale = getOption("piecepackr.op_scale", 0),
op_angle = getOption("piecepackr.op_angle", 45),
inherit.aes = TRUE
)
aes_piece(df)
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
stat |
The statistical transformation to use on the data for this layer.
When using a
|
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
|
... |
Aesthetics, used to set an aesthetic to a fixed value. |
envir |
Environment (or named list) containing configuration list(s). |
op_scale |
How much to scale the depth of the piece in the oblique projection
(viewed from the top of the board).
|
op_angle |
What is the angle of the oblique projection? Has no effect if |
inherit.aes |
If |
df |
A data frame of game piece information with (at least) the named columns “piece_side”, “x”, and “y”. |
geom_piece()
requires a fixed scale coordinate system with an aspect
ratio of 1 as provided by ggplot2::coord_fixed()
.
geom_piece()
also requires that cfg
is a character vector (and not a pp_cfg()
object).
In particular if using op_transform()
one should set its argument cfg_class = "character"
if intending for use with geom_piece()
.
geom_piece()
understands the following aesthetics (required aesthetics are in bold).
See pieceGrob()
for more details.
x
y
z
piece_side
rank
suit
cfg
width
height
depth
angle
scale
type
geom_piece()
is a wrapper around pieceGrob()
.
scale_x_piece()
and scale_y_piece()
are wrappers
around ggplot2::scale_x_continuous()
and ggplot2::scale_y_continuous()
with better defaults for board game diagrams.
if (require("ggplot2", quietly = TRUE) && require("tibble", quietly = TRUE)) {
envir <- game_systems("sans")
df_board <- tibble(piece_side = "board_face", suit = 3, rank = 8,
x = 4.5, y = 4.5)
df_w <- tibble(piece_side = "bit_face", suit = 6, rank = 1,
x = rep(1:8, 2), y = rep(1:2, each=8))
df_b <- tibble(piece_side = "bit_face", suit = 1, rank = 1,
x = rep(1:8, 2), y = rep(7:8, each=8))
df <- rbind(df_board, df_w, df_b)
# 2D example
# `cfg` must be a character vector for `geom_piece()`
ggplot(df, aes_piece(df)) +
geom_piece(cfg = "checkers1", envir = envir) +
coord_fixed() +
scale_x_piece() +
scale_y_piece() +
theme_minimal(28) +
theme(panel.grid = element_blank())
}
if (require("ggplot2", quietly = TRUE) && require("tibble", quietly = TRUE)) {
# 3D "oblique" projection example
# `cfg_class` must be "character" when using with `geom_piece()`
df3d <- op_transform(df, cfg = "checkers1", envir = envir,
op_angle = 45, cfg_class = "character")
ggplot(df3d, aes_piece(df3d)) +
geom_piece(cfg = "checkers1", envir = envir,
op_angle = 45, op_scale = 0.5) +
coord_fixed() +
theme_void()
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.