geom_mosaic_text: Labeling for Mosaic plots.

Description Usage Arguments Examples

View source: R/geom-mosaic-text.R

Description

A mosaic plot with text or labels

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
geom_mosaic_text(
  mapping = NULL,
  data = NULL,
  stat = "mosaic",
  position = "identity",
  na.rm = FALSE,
  divider = mosaic(),
  offset = 0.01,
  show.legend = NA,
  inherit.aes = FALSE,
  as.label = FALSE,
  repel = FALSE,
  repel_params = NULL,
  check_overlap = FALSE,
  ...
)

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), removes missing values with a warning. If TRUE silently removes missing values.

divider

Divider function. The default divider function is mosaic() which will use spines in alternating directions. The four options for partitioning:

  • vspine Vertical spine partition: width constant, height varies.

  • hspine Horizontal spine partition: height constant, width varies.

  • vbar Vertical bar partition: height constant, width varies.

  • hbar Horizontal bar partition: width constant, height varies.

offset

Set the space between the first spine

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

as.label

Show as a ggplot label (box with round corners)

repel

Use ggrepel wo labels don't overlap

repel_params

List of ggrepel parameters (e.g. list(point.padding = 0))

check_overlap

If TRUE, text that overlaps previous text in the same layer will not be plotted. check_overlap happens at draw time and in the order of the data. Therefore data should be arranged by the label column before calling geom_label() or geom_text().

...

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

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
data(titanic)

ggplot(data = titanic) +
  geom_mosaic(aes(x = product(Class), fill = Survived)) +
  geom_mosaic_text(aes(x = product(Class), fill = Survived))

ggplot(data = titanic) +
  geom_mosaic(aes(x = product(Class, Sex),  fill = Survived),
              divider = c("vspine", "hspine", "hspine")) +
  geom_mosaic_text(aes(x = product(Class, Sex), fill = Survived),
              divider = c("vspine", "hspine", "hspine"), size = 2)

ggplot(data = happy) +
  geom_mosaic(aes(x = product(health), fill = happy), na.rm = TRUE, show.legend = FALSE) +
  geom_mosaic_text(aes(x = product(happy, health)), na.rm = TRUE)

# avoid overlapping text
ggplot(data = happy) +
  geom_mosaic(aes(x = product(health), fill = happy), na.rm = TRUE, show.legend = FALSE) +
  geom_mosaic_text(aes(x = product(happy, health)), na.rm = TRUE, check_overlap = TRUE)

# or use ggrepel
ggplot(data = happy) +
  geom_mosaic(aes(x = product(health), fill = happy), na.rm = TRUE, show.legend = FALSE) +
  geom_mosaic_text(aes(x = product(happy, health)), na.rm = TRUE, repel = TRUE)

# and as a label
ggplot(data = happy) +
  geom_mosaic(aes(x = product(health), fill = happy), na.rm = TRUE, show.legend = FALSE) +
  geom_mosaic_text(aes(x = product(happy, health)), na.rm = TRUE, repel = TRUE, as.label=TRUE)

ggmosaic documentation built on Feb. 24, 2021, 1:06 a.m.