geom_pie_glyph {PieGlyph}R Documentation

Scatter plot with points replaced by axis-invariant pie-chart glyphs

Description

This geom replaces the points in a scatter plot with pie-chart glyphs showing the relative proportions of different categories. The pie-chart glyphs are independent of the plot dimensions, so won't distort when the plot is scaled. The ideal dataset for this geom would contain columns with non-negative values showing the magnitude of the different categories to be shown in the pie glyphs (The proportions of the different categories within the pie glyph will be calculated automatically). The different categories can also be stacked together into a single column according to the rules of tidy-data (see vignette('tidy-data') or vignette('pivot') for more information).

Usage

geom_pie_glyph(
  mapping = NULL,
  data = NULL,
  slices,
  values = NA,
  stat = "identity",
  position = "identity",
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE,
  ...
)

Arguments

mapping

Set of aesthetic (see Aesthetics below) mappings to be 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 of the plot.
The default, NULL, inherits the plot data specified in the ggplot() call.
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)).

slices

Each pie glyph in the plot shows the relative abundances of a set of categories; those categories are specified by this argument and should contain numeric and non-negative values. The names of the categories can be the names of individual columns (wide format) or can be stacked and contained in a single column (long format using pivot_longer()). The categories can also be specified as the numeric indices of the columns.

values

This parameter is not needed if the data is in wide format. The default is NA assuming that the categories are in separate columns. However, if the pie categories are stacked in one column, this parameter describes the column for the values of the categories shown in the pie glyphs. The values should be numeric and non-negative and the proportions of the different slices within each observation will be calculated automatically.

stat

The statistical transformation to use on the data for this layer, either as a ggproto Geom subclass or as a string naming the stat stripped of the stat_ prefix (e.g. "count" rather than "stat_count")

position

Position adjustment, either as a string naming the adjustment (e.g. "jitter" to use position_jitter), or the result of a call to a position adjustment function. Use the latter if you need to change the settings of the adjustment.

na.rm

If all slices for an observation are NA, the observation is dropped while if at least one slice is not NA, the other slices are assumed to be 0. This parameter indicates whether the user is notified about these changes. If FALSE, the default, user is given a warning. If TRUE, observations are silently removed/modified to 0, without notifying the user.

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.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them

...

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

Value

A ggplot layer

Aesthetics

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

Examples


## Load libraries
library(tidyverse)
library(PieGlyph)

## Simulate raw data
set.seed(123)
plot_data <- data.frame(response = rnorm(10, 100, 30),
                        system = 1:10,
                        group = sample(size = 10,
                                       x = c('G1', 'G2', 'G3'),
                                       replace = TRUE),
                        A = round(runif(10, 3, 9), 2),
                        B = round(runif(10, 1, 5), 2),
                        C = round(runif(10, 3, 7), 2),
                        D = round(runif(10, 1, 9), 2))

head(plot_data)

## Basic plot
ggplot(data = plot_data, aes(x = system, y = response))+
   geom_pie_glyph(slices = c('A', 'B', 'C', 'D'),
                  data = plot_data)+
   theme_classic()


## Change pie radius and border colour
ggplot(data = plot_data, aes(x = system, y = response))+
       # Can also specify slices as column indices
       geom_pie_glyph(slices = 4:7, data = plot_data,
                      colour = 'black', radius = 0.5)+
       theme_classic()


## Map radius to a variable
p <- ggplot(data = plot_data, aes(x = system, y = response))+
geom_pie_glyph(aes(radius = group),
            slices = c('A', 'B', 'C', 'D'),
            data = plot_data, colour = 'black')+
            theme_classic()
p


## Add custom labels
p <- p + labs(x = 'System', y = 'Response',
              fill = 'Attributes', radius = 'Group')
p


## Change slice colours
p + scale_fill_manual(values = c('#56B4E9', '#CC79A7',
                                 '#F0E442', '#D55E00'))



##### Stack the attributes in one column
# The attributes can also be stacked into one column to generate
# the plot. This variant of the function is useful for situations
# when the data is in tidy format. See vignette('tidy-data') and
# vignette('pivot') for more information.

plot_data_stacked <- plot_data %>%
                        pivot_longer(cols = c('A','B','C','D'),
                                     names_to = 'Attributes',
                                     values_to = 'values')
head(plot_data_stacked, 8)


ggplot(data = plot_data_stacked, aes(x = system, y = response))+
  # Along with slices column, values column is also needed now
  geom_pie_glyph(slices = 'Attributes', values = 'values')+
  theme_classic()

[Package PieGlyph version 0.1.0 Index]