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 |
data |
The data to be displayed in this layer of the plot. |
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
|
values |
This parameter is not needed if the data is in wide format.
The default is |
stat |
The statistical transformation to use on the data for this layer,
either as a |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
na.rm |
If all slices for an observation are |
show.legend |
Logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Other arguments passed on to layer(). These are often aesthetics,
used to set an aesthetic to a fixed value, like |
Value
A ggplot layer
Aesthetics
geom_pie_glyph understands the following aesthetics (required aesthetics are in bold):
-
x - variable to be shown on X-axis.
-
y - variable to be shown on Y-axis.
alpha - adjust opacity of the pie glyphs.
radius - adjust the radius of the pie glyphs (in cm).
colour - specify colour of the border of pie glyphs.
linetype - specify style of pie glyph borders.
linewidth - specify width of pie glyph borders (in mm).
group - specify grouping structure for the observations (see
grouping
for more details).pie_group - manually specify a grouping variable for separating pie-glyphs with identical x and y coordinates (see
vignette("unusual-situations")
for more information).
Examples
## Load libraries
library(dplyr)
library(tidyr)
library(ggplot2)
## Simulate raw data
set.seed(123)
plot_data <- data.frame(response = rnorm(10, 100, 30),
system = as.factor(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 using `radius` and border colour using `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()