geom_chaikin {ggpointless} | R Documentation |
Apply Chaikin's corner cutting algorithm to smooth a path
Description
Chaikin's corner-cutting algorithm can be used to smooth sharp corners of a path.
Usage
geom_chaikin(
mapping = NULL,
data = NULL,
stat = "chaikin",
position = "identity",
...,
iterations = 5,
ratio = 0.25,
closed = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
stat_chaikin(
mapping = NULL,
data = NULL,
geom = "path",
position = "identity",
...,
iterations = 5,
ratio = 0.25,
closed = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
... |
Other arguments passed on to |
iterations |
Integer. Number of iterations to apply. Must be between 0 and 10. |
ratio |
Numeric. Cutting ratio must be between 0 and 1. |
closed |
Logical. Specify if result is an open or closed shape. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
geom , stat |
Use to override the default connection between
|
Details
Chaikin's corner cutting algorithm iteratively turns a jagged path into a smooth path.
The recursion formula starts from two vertices A and B, which represent a single corner of your path. From this, the algorithm derives two new points: one at the specified ratio when going from point A to point B, and one when going from B to A in the opposite direction. By default, a ratio of 0.25 results in two points: the first at 25% of point A and the other at 75% of point A (or 25% of point B). Those new points form a smoother path. Then the algorithm applies the same rule to each pair of new points. The rule is applied iterations times. The maximum number of iterations is 10, default is 5.
The ratio parameter must be a number between 0 and 1. If ratio > 0.5, then it will be flipped to 1 - ratio, and a message is shown.
Aesthetics
geom_chaikin()
understands the following aesthetics (required
aesthetics are in bold):
-
x
-
y
alpha
color
group
linetype
linewidth
References
Chaikin, G. An algorithm for high speed curve generation. Computer Graphics and Image Processing 3 (1974), 346–349
Examples
set.seed(42)
dat <- data.frame(
x = seq.int(10),
y = sample(15:30, 10)
)
p1 <- ggplot(dat, aes(x, y)) +
geom_line(linetype = "12")
p1 +
geom_chaikin()
p1 +
geom_chaikin(iterations = 1)
triangle <- data.frame(x = c(0, 0, 1), y = c(0, 1, 1))
p2 <- ggplot(triangle, aes(x, y)) +
geom_path(linetype = "12") +
coord_equal()
# ratio let's you control
p2 + geom_chaikin(ratio = .1)
p2 + geom_chaikin(ratio = .5)
# closed parameter to generate a closed shape - or not
p2 + geom_chaikin(iterations = 5, ratio = 0.25, closed = FALSE) # default
p2 + geom_chaikin(closed = TRUE)