Skip to contents

The pieglyph geom is used to plot multivariate data as pie glyphs (Ward and Lipchak 2000; Fuchs et al. 2013) in a scatterplot.

Usage

geom_pieglyph(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  cols = character(0L),
  edges = 200,
  fill.segment = NULL,
  fill.gradient = NULL,
  colour.grid = NULL,
  linewidth = 1,
  linewidth.grid = linewidth,
  scale.segment = FALSE,
  scale.radius = TRUE,
  full = TRUE,
  draw.grid = FALSE,
  legend.glyph.dims = setNames(rep(0.5, length(cols)), cols),
  show.legend = NA,
  repel = FALSE,
  repel.control = ggmultiglyph.repel.control(),
  inherit.aes = TRUE
)

Arguments

mapping

Set of aesthetic mappings created by 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. When using a geom_*() function to construct a layer, the stat argument can be used to override the default coupling between geoms and stats. The stat argument accepts the following:

  • A Stat ggproto subclass, for example StatCount.

  • A string naming the stat. To give the stat as a string, strip the function name of the stat_ prefix. For example, to use stat_count(), give the stat as "count".

  • For more information and other ways to specify the stat, see the layer stat documentation.

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 position argument accepts the following:

  • The result of calling a position function, such as position_jitter(). This method allows for passing extra arguments to the position.

  • A string naming the position adjustment. To give the position as a string, strip the function name of the position_ prefix. For example, to use position_jitter(), give the position as "jitter".

  • For more information and other ways to specify the position, see the layer position documentation.

...

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

cols

A character vector containing the names of at least two columns specifying the variables to be plotted in the glyphs. The selected columns must be either numeric or factor variables.

edges

The number of edges of the polygon to depict the circular glyph outline.

fill.segment

The fill colour of the segments.

fill.gradient

The palette for gradient fill of the segments. See Details section of col_numeric() function in the scales package for available options.

colour.grid

The colour of grid lines.

linewidth

The line width of the segments.

linewidth.grid

The line width for the grid lines.

scale.segment

logical. If TRUE, the segments (pie slices) are scaled according to value of cols.

scale.radius

logical. If TRUE, the radius of segments (pie slices) are scaled according to value of cols.

full

logical. If TRUE, full star glyphs (360°) are plotted, otherwise half star glyphs (180°) are plotted.

draw.grid

logical. If TRUE, grid levels are plotted along the sectors if all the variables specified in cols are an ordered factor. Default is FALSE.

legend.glyph.dims

The dimensions of the legend glyph plot. Can be a numeric vector of unit length (where all the dimensions will have same value) or a numeric vector of same length as "cols" with the "cols" as names.

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. To include legend keys for all levels, even when no data exists, use TRUE. If NA, all levels are shown in legend, but unobserved levels are omitted.

repel

logical. If TRUE, the glyphs are repel away from each other to avoid overlaps. Default is FALSE.

repel.control

A list of control settings for the repel algorithm. Ignored if repel = FALSE. See ggmultiglyph.repel.control for details on the various control parameters.

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

Value

A geom layer.

Aesthetics

geom_pieglyph() understands the following aesthetics (required aesthetics are in bold):

  • x

  • y

  • alpha

  • colour

  • fill

  • group

  • size

See vignette("ggplot2-specs", package = "ggplot2") for further details on setting these aesthetics.

The following additional aesthetics are considered if repel = TRUE:

  • point.size

  • segment.linetype

  • segment.colour

  • segment.size

  • segment.alpha

  • segment.curvature

  • segment.angle

  • segment.ncp

  • segment.shape

  • segment.square

  • segment.squareShape

  • segment.inflect

  • segment.debug

See ggrepel examples page for further details on setting these aesthetics.

References

Fuchs J, Fischer F, Mansmann F, Bertini E, Isenberg P (2013). “Evaluation of alternative glyph designs for time series data in a small multiple setting.” In Proceedings of the SIGCHI Conference on Human Factors in Computing Systems, 3237–3246. ISBN 978-1-4503-1899-0.

Ward MO, Lipchak BN (2000). “A visualization tool for exploratory analysis of cyclic multivariate data.” Metrika, 51(1), 27–37.

Examples



library(ggplot2)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Prepare the data ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Variables to map to glyphs
zs <- c("hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb")

# Keep a copy of the original data
mtcars_fct <- mtcars

# Scaled numeric data
mtcars[zs] <- lapply(mtcars[zs], scales::rescale)

mtcars$cyl <- factor(mtcars$cyl)
mtcars$lab <- row.names(mtcars)

# Ordered factor data
mtcars_fct[zs[1:3]] <-
  lapply(mtcars_fct[zs[1:3]], function(x)
    ordered(cut(x, breaks = 3,
                labels = c("low", "medium", "high"))))

mtcars_fct[zs[4:8]] <-
  lapply(mtcars_fct[zs[4:8]], function(x)
    ordered(cut(x, breaks = 4,
                labels = c("tiny", "small", "medium", "large"))))

mtcars_fct$cyl <- factor(mtcars_fct$cyl)
mtcars_fct$lab <- row.names(mtcars_fct)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Mapped fill + scaled radius ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 10,
                alpha =  0.8) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 10,
                alpha =  0.8, full = FALSE) +
  ylim(c(-0, 550))


# \donttest{
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Mapped fill + scaled segment ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = TRUE,
                alpha =  0.8) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = TRUE,
                alpha =  0.8, full = FALSE) +
  ylim(c(-0, 550))


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Mapped colour + scaled radius ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, colour = cyl),
                cols = zs, size = 10,
                alpha =  0.8) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, colour = cyl),
                cols = zs, size = 10, fill = "white",
                alpha =  0.8,  linewidth = 2) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, colour = cyl),
                cols = zs, size = 10,
                alpha =  0.8, full = FALSE) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, colour = cyl),
                cols = zs, size = 10, fill = "white",
                alpha =  0.8,  linewidth = 2, full = FALSE) +
  ylim(c(-0, 550))


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Mapped colour + scaled segment ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, colour = cyl),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = TRUE,
                alpha =  0.8) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, colour = cyl),
                cols = zs, size = 5, fill = "white",
                scale.radius = FALSE, scale.segment = TRUE,
                alpha =  0.8, linewidth = 2) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, colour = cyl),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = TRUE,
                alpha =  0.8, full = FALSE) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, colour = cyl),
                cols = zs, size = 5, fill = "white",
                scale.radius = FALSE, scale.segment = TRUE,
                alpha =  0.8, linewidth = 2, full = FALSE) +
  ylim(c(-0, 550))


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Segments with multivariate colours + scaled radius ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 10,
                fill.segment = RColorBrewer::brewer.pal(8, "Dark2"),
                alpha =  0.8) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 10,
                fill.segment = RColorBrewer::brewer.pal(8, "Dark2"),
                alpha =  0.8, full = FALSE) +
  ylim(c(-0, 550))


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Segments with multivariate colours + scaled segment (scatterpie) ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = TRUE,
                fill.segment = RColorBrewer::brewer.pal(8, "Dark2"),
                alpha =  0.8) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = TRUE,
                fill.segment = RColorBrewer::brewer.pal(8, "Dark2"),
                alpha =  0.8, full = FALSE) +
  ylim(c(-0, 550))


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Gradient fill ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = FALSE,
                fill.gradient = "Greens",
                alpha =  0.8) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = FALSE,
                fill.gradient = "Blues",
                alpha =  0.8) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = FALSE,
                fill.gradient = "RdYlBu",
                alpha =  0.8) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = FALSE,
                fill.gradient = "viridis",
                alpha =  0.8) +
  ylim(c(-0, 550))


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Faceted ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 10,
                alpha =  0.8) +
  ylim(c(-0, 550)) +
  facet_grid(. ~ cyl)


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp, colour = cyl),
                cols = zs, size = 10,
                alpha =  0.8) +
  ylim(c(-0, 550)) +
  facet_grid(. ~ cyl)


ggplot(data = mtcars) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 10,
                fill.segment = RColorBrewer::brewer.pal(8, "Dark2"),
                alpha =  0.8) +
  ylim(c(-0, 550)) +
  facet_grid(. ~ cyl)


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Repel glyphs ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl)) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 10,
                alpha =  1, repel = TRUE) +
  ylim(c(-0, 550))
#> Warning: 6 glyphs have too many overlaps.
#> Consider increasing "max.overlaps"


ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl)) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = TRUE,
                alpha =  1, full = FALSE, repel = TRUE) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl)) +
  geom_pieglyph(aes(x = mpg, y = disp, colour = cyl),
                cols = zs, size = 10,
                alpha =  1, repel = TRUE) +
  ylim(c(-0, 550))
#> Warning: 6 glyphs have too many overlaps.
#> Consider increasing "max.overlaps"


ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl)) +
  geom_pieglyph(aes(x = mpg, y = disp, colour = cyl),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = TRUE,
                alpha =  1, full = FALSE, repel = TRUE) +
  ylim(c(-0, 550))


ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp)) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 10,
                fill.segment = RColorBrewer::brewer.pal(8, "Dark2"),
                alpha =  1, repel = TRUE) +
  ylim(c(-0, 550))
#> Warning: 5 glyphs have too many overlaps.
#> Consider increasing "max.overlaps"


ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp)) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 5,
                scale.radius = FALSE, scale.segment = FALSE,
                fill.gradient = "viridis",
                alpha =  1, repel = TRUE) +
  ylim(c(-0, 550))


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Grid lines (when scale.radius = TRUE) ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ggplot(data = mtcars_fct) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 2,
                alpha =  0.8, draw.grid = TRUE) +
  ylim(c(-0, 550))


ggplot(data = mtcars_fct) +
  geom_pieglyph(aes(x = mpg, y = disp, colour = cyl),
                cols = zs, size = 2,
                alpha =  0.8, draw.grid = TRUE) +
  ylim(c(-0, 550))


ggplot(data = mtcars_fct) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 2,
                scale.radius = TRUE, scale.segment = FALSE,
                fill.gradient = "Blues",
                alpha =  0.8, draw.grid = TRUE) +
  ylim(c(-0, 550))


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Legend options ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Default legend/guide
ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl)) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 5,
                alpha =  1, repel = TRUE) +
  ylim(c(-0, 550))


# Theme modifications for legend
legend_theme <-
  theme_bw(base_size = 7.5) +
  theme(legend.direction = "vertical",
        legend.box = "horizontal",
        legend.position = "bottom",
        legend.text = element_text(margin = margin(l = 7)),
        legend.key.height = unit(1.5, 'lines'))

# Glyph variable-wise legends
ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl), show.legend = FALSE) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 5,
                alpha =  1, repel = TRUE) +
  ylim(c(-0, 550)) +
  scale_z_continuous(z = zs) +
  guide_z_order(z = zs, default_aes = "fill") +
  legend_theme


ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl), show.legend = FALSE) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 3,
                scale.radius = FALSE, scale.segment = TRUE,
                fill.segment = RColorBrewer::brewer.pal(8, "Dark2"),
                alpha =  1, repel = TRUE) +
  ylim(c(-0, 550)) +
  scale_z_continuous(z = zs) +
  guide_z_order(z = zs, default_aes = "fill") +
  legend_theme


# Modifying the `legend.glyph.dims`
zavg <- # Scaled average of the variables
  apply(mtcars[, zs], 2,
        function(x) {
          scales::rescale(mean(x),
                          from = range(x), # same range as in scale_z_continuous
                          to = c(0, 1))
        })
zavg
#>        hp      drat        wt      qsec        vs        am      gear      carb 
#> 0.3345848 0.3855127 0.4357581 0.3986607 0.4375000 0.4062500 0.3437500 0.2589286 

ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl)) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 5,
                alpha =  1, repel = TRUE,
                legend.glyph.dims = zavg) +
  ylim(c(-0, 550)) +
  xlim(c(8, 35))


ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl), show.legend = FALSE) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 5,
                alpha =  1, repel = TRUE,
                legend.glyph.dims = zavg) +
  ylim(c(-0, 550)) +
  scale_z_continuous(z = zs) +
  guide_z_order(z = zs, default_aes = "fill")  +
  legend_theme


ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl), show.legend = FALSE) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 3,
                scale.radius = FALSE, scale.segment = TRUE,
                fill.segment = RColorBrewer::brewer.pal(8, "Dark2"),
                alpha =  1, repel = TRUE,
                legend.glyph.dims = zavg) +
  ylim(c(-0, 550)) +
  scale_z_continuous(z = zs) +
  guide_z_order(z = zs, default_aes = "fill")  +
  legend_theme


# Using custom guide
# pieglyphGrob
guide_piegrob <- pieglyphGrob(
  z = c(0.24, 0.3, 0.8, 1.4, 0.6, 0.33, 0.6, 0.25) * 0.75,
  size = 25)
# Add labels to pieglyphGrob
guide_piegrob <-
  addlabel.glyphGrob(grob = guide_piegrob, label = zs,
                     push = 1, segment = FALSE)

# Another version
guide_piegrob2 <- pieglyphGrob(
  z = rep(0.5, length(zs)),
  size = 25)
guide_piegrob2 <-
  addlabel.glyphGrob(grob = guide_piegrob2, label = zs,
                     push = 1, segment = FALSE)

# Multivariate colour version
guide_piegrob_mcol <- pieglyphGrob(
  z = rep(0.5, length(zs)),
  size = 25, fill = RColorBrewer::brewer.pal(8, "Dark2"))
guide_piegrob_mcol <-
  addlabel.glyphGrob(grob = guide_piegrob_mcol, label = zs,
                     push = 1, segment = FALSE)

ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl), show.legend = FALSE) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 5,
                alpha =  1, repel = TRUE) +
  ylim(c(-0, 550)) +
  guides(fill = guide_legend(order = 1, position = "right"),
         custom = guide_custom(guide_piegrob,
                               width = unit(0.1, "npc"),
                               height = unit(0.1, "npc"),
                               position = "bottom",
                               theme = theme(legend.margin = margin(t = 40, b = 30))))


ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl), show.legend = FALSE) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 5,
                alpha =  1, repel = TRUE) +
  ylim(c(-0, 550)) +
  guides(fill = guide_legend(order = 1, position = "right"),
         custom = guide_custom(guide_piegrob2,
                               width = unit(0.1, "npc"),
                               height = unit(0.1, "npc"),
                               position = "bottom",
                               theme = theme(legend.margin = margin(t = 30, b = 30))))


ggplot(data = mtcars) +
  geom_point(aes(x = mpg, y = disp, colour = cyl), show.legend = FALSE) +
  geom_pieglyph(aes(x = mpg, y = disp),
                cols = zs, size = 3,
                scale.radius = FALSE, scale.segment = TRUE,
                fill.segment = RColorBrewer::brewer.pal(8, "Dark2"),
                alpha =  1, repel = TRUE) +
  ylim(c(-0, 550)) +
  guides(fill = guide_legend(order = 1, position = "right"),
         custom = guide_custom(guide_piegrob_mcol,
                               width = unit(0.1, "npc"),
                               height = unit(0.1, "npc"),
                               position = "bottom",
                               theme = theme(legend.margin = margin(t = 30, b = 30))))


# Legend in plots with grid lines (when scale.radius = TRUE)

z_grid <- c(hp = 3, drat = 3, wt = 2,
            qsec = 2, vs = 3, am = 4,
            gear = 2, carb = 3)

ggplot(data = mtcars_fct) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 1,
                alpha =  0.8, draw.grid = TRUE) +
  ylim(c(-0, 550))


ggplot(data = mtcars_fct) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 1,
                alpha =  0.8, draw.grid = TRUE) +
  ylim(c(-0, 550)) +
  scale_z_discrete(z = zs) +
  guide_z_order(z = zs, default_aes = "fill") +
  legend_theme


ggplot(data = mtcars_fct) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 1,
                alpha =  0.8, draw.grid = TRUE,
                legend.glyph.dims = z_grid) +
  ylim(c(-0, 550))


ggplot(data = mtcars_fct) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 1,
                alpha =  0.8, draw.grid = TRUE,
                legend.glyph.dims = z_grid) +
  ylim(c(-0, 550)) +
  scale_z_discrete(z = zs) +
  guide_z_order(z = zs, default_aes = "fill") +
  legend_theme


# pieglyphGrob with grid levels

z_grid_levels <- lapply(z_grid, function(x) 1:x)

guide_piegrob_grid <-
  pieglyphGrob(z = z_grid,
               size = 3, draw.grid = TRUE,
               col.grid = "black",
               grid.levels = z_grid_levels)

guide_piegrob_grid <-
  addlabel.glyphGrob(grob = guide_piegrob_grid, label = zs,
                     push = 1, segment = FALSE)

# Another version
guide_piegrob_grid2 <-
  pieglyphGrob(z = rep(3, length(z_grid)),
               size = 3, draw.grid = TRUE,
               col.grid = "black",
               grid.levels = replicate(8, 1:3,
                                       simplify = FALSE))
guide_piegrob_grid2 <-
  addlabel.glyphGrob(grob = guide_piegrob_grid2, label = zs,
                     push = 1, segment = FALSE)

ggplot(data = mtcars_fct) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 1,
                alpha =  0.8, draw.grid = TRUE,
                legend.glyph.dims = z_grid) +
  ylim(c(-0, 550)) +
  guides(fill = guide_legend(order = 1, position = "right"),
         custom = guide_custom(guide_piegrob_grid,
                               width = unit(0.1, "npc"),
                               height = unit(0.1, "npc"),
                               position = "bottom",
                               theme = theme(legend.margin = margin(t = 20, b = 30))))


ggplot(data = mtcars_fct) +
  geom_pieglyph(aes(x = mpg, y = disp, fill = cyl),
                cols = zs, size = 1,
                alpha =  0.8, draw.grid = TRUE,
                legend.glyph.dims = z_grid) +
  ylim(c(-0, 550)) +
  guides(fill = guide_legend(order = 1, position = "right"),
         custom = guide_custom(guide_piegrob_grid2,
                               width = unit(0.1, "npc"),
                               height = unit(0.1, "npc"),
                               position = "bottom",
                               theme = theme(legend.margin = margin(t = 20, b = 30))))


# }