geom_moon {gggibbous} | R Documentation |
Moon charts
Description
The moon geom is used to create moon charts, which are like pie charts except that the proportions are shown as crescent or gibbous portions of a circle, like the lit and unlit portions of the moon. As such, they work best with only one or two groups.
Usage
geom_moon(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
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 |
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. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Other arguments passed on to |
Details
geom_moon
acts like geom_point
in that multiple moons can be
plotted on the same panel with x
and y
in the plot's coordinate
system, but size determined independently of the coordinate system. This
behavior also means that the moons will always be circular even if the
coordinate system is not square.
In order to get a full circle with two complementary sections (a crescent and
a gibbous moon), you need to plot two shapes: one with right = TRUE
and one with right = FALSE
, with ratio
on the second one equal
to 1 - ratio
on the first.
Aesthetics
x
and y
are required aesthetics.
size
, fill
,
colo(u)r
, alpha
, stroke
, and group
aesthetics
are understood as in other geom
s.
Two new aesthetics are also introduced: ratio
and right
.
ratio
controls the proportion of the moon to be plotted, from 0 to 1.
right
takes a boolean value to indicate whether the moon should be
filled from the right or the left.
Examples
ggplot(
data.frame(x = 1:5, y = 1, size = 1:5, ratio = 1:5 * 0.2),
aes(x = x, y = y, size = size, ratio = ratio)
) +
geom_moon()
# To make full moon charts, you need two calls to geom_moon(), one with
# right = TRUE and one with right = FALSE and ratio equal to 1 - ratio
# from the first one
ggplot(dmeladh) +
geom_moon(
x = 0.5, y = 0.5, fill = "forestgreen", color = "forestgreen",
aes(ratio = AdhF / 100)
) +
geom_moon(
x = 0.5, y = 0.5, fill = "gold", color = "gold",
aes(ratio = AdhS / 100), right = FALSE
) +
facet_wrap(~Locality, ncol = 7)
# The same thing can be accomplished with a single call to geom_moon()
# using a "long" data frame with both frequencies if you set a grouping
# variable and set the `right` variable to a boolean column
dmeladh_long <- reshape(
dmeladh,
varying = c("AdhF", "AdhS"),
v.names = "freq",
timevar = "allele",
times = c("AdhF", "AdhS"),
idvar = c("Locality", "Latitude", "Longitude", "N"),
direction = "long"
)
dmeladh_long$right <- rep(c(TRUE, FALSE), each = nrow(dmeladh))
ggplot(dmeladh_long) +
geom_moon(
x = 0.5, y = 0.5, key_glyph = draw_key_rect,
aes(ratio = freq / 100, fill = allele, color = allele, right = right),
) +
facet_wrap(~Locality, ncol = 7)
# Moon charts (and pie charts) are sometimes useful on maps when x and y
# cannot be used as aesthetic dimensions because they are already spatial
# dimensions. Overplotting needs to be considered carefully, however.
ggplot(
subset(dmeladh, N > 200),
aes(Longitude, Latitude)
) +
geom_moon(aes(ratio = AdhF / 100), fill = "black") +
geom_moon(aes(ratio = AdhS / 100), right = FALSE) +
coord_fixed()