geom_moon: Moon charts

Description Usage Arguments Details Aesthetics Examples

View source: R/geom_moon.R

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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 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.

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

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().

...

Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. They may also be parameters to the paired geom/stat.

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 geoms. 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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()

gggibbous documentation built on Jan. 13, 2021, 6:51 a.m.