The starglyph geom is used to plot multivariate data as star glyphs (Siegel et al. 1972; Chambers et al. 1983; duToit et al. 1986) in a scatterplot.
Usage
geom_starglyph(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
cols = character(0L),
whisker = TRUE,
contour = TRUE,
colour.whisker = NULL,
colour.contour = NULL,
colour.points = NULL,
linewidth.whisker = 1,
linewidth.contour = 1,
full = TRUE,
draw.grid = FALSE,
grid.point.size = 1,
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 andinherit.aes = TRUE(the default), it is combined with the default mapping at the top level of the plot. You must supplymappingif 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 toggplot().A
data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. Seefortify()for which variables will be created.A
functionwill be called with a single argument, the plot data. The return value must be adata.frame, and will be used as the layer data. Afunctioncan be created from aformula(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, thestatargument can be used to override the default coupling between geoms and stats. Thestatargument accepts the following:A
Statggproto subclass, for exampleStatCount.A string naming the stat. To give the stat as a string, strip the function name of the
stat_prefix. For example, to usestat_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
positionargument 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 useposition_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, likecolour = "green"orsize = 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.
- whisker
logical. If
TRUE, plots the star glyph whiskers.- contour
logical. If
TRUE, plots the star glyph contours.- colour.whisker
The colour of whiskers.
- colour.contour
The colour of contours.
- colour.points
The colour of grid points.
- linewidth.whisker
The whisker line width.
- linewidth.contour
The contour line width.
- full
logical. If
TRUE, full star glyphs (360°) are plotted, otherwise half star glyphs (180°) are plotted.- draw.grid
logical. If
TRUE, grid points are plotted along the whiskers if all the variables specified incolsare an ordered factor. Default isFALSE.- grid.point.size
The size of the grid points in native units.
- 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.FALSEnever includes, andTRUEalways 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, useTRUE. IfNA, 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 isFALSE.- repel.control
A list of control settings for the repel algorithm. Ignored if
repel = FALSE. Seeggmultiglyph.repel.controlfor 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().
Aesthetics
geom_starglyph() understands the following
aesthetics (required aesthetics are in bold):
x
y
alpha
colour
fill
group
shape
size
stroke
linetype
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
Chambers JM, Cleveland WS, Kleiner B, Tukey PA (1983).
Graphical Methods for Data Analysis.
Chapman and Hall/CRC, Boca Raton.
ISBN 978-1-351-07230-4.
duToit SHC, Steyn AGW, Stumpf RH (1986).
Graphical Exploratory Data Analysis, Springer Texts in Statistics.
Springer-Verlag, New York.
ISBN 978-1-4612-9371-2.
Siegel JH, Farrell EJ, Goldwyn RM, Friedman HP (1972).
“The surgical implications of physiologic patterns in myocardial infarction shock.”
Surgery, 72(1), 126–141.
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)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Both whiskers and contour ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot(data = mtcars) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 10, alpha = 0.5) +
ylim(c(-0, 550))
ggplot(data = mtcars) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 10, alpha = 0.5, full = FALSE) +
ylim(c(-0, 550))
ggplot(data = mtcars) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 10, alpha = 0.5,
linewidth.whisker = 3, linewidth.contour = 0.1) +
ylim(c(-0, 550))
ggplot(data = mtcars) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 10, alpha = 0.5,
linewidth.whisker = 1, linewidth.contour = 3) +
ylim(c(-0, 550))
# \donttest{
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Only contours (polygon) ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot(data = mtcars) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = FALSE, contour = TRUE,
size = 10, alpha = 0.5) +
ylim(c(-0, 550))
ggplot(data = mtcars) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = FALSE, contour = TRUE,
size = 10, alpha = 0.5, linewidth.contour = 3) +
ylim(c(-0, 550))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Only whiskers ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot(data = mtcars) +
geom_starglyph(aes(x = mpg, y = disp, colour = cyl),
cols = zs, whisker = TRUE, contour = FALSE,
size = 10) +
geom_point(data = mtcars, aes(x = mpg, y = disp, colour = cyl)) +
ylim(c(-0, 550))
ggplot(data = mtcars) +
geom_starglyph(aes(x = mpg, y = disp, colour = cyl),
cols = zs, whisker = TRUE, contour = FALSE,
size = 10, full = FALSE) +
geom_point(data = mtcars, aes(x = mpg, y = disp, colour = cyl)) +
ylim(c(-0, 550))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Whiskers with multivariate colours ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot(data = mtcars) +
geom_starglyph(aes(x = mpg, y = disp),
cols = zs, whisker = TRUE, contour = FALSE,
size = 10,
colour.whisker = RColorBrewer::brewer.pal(8, "Dark2")) +
geom_point(data = mtcars, aes(x = mpg, y = disp)) +
ylim(c(-0, 550))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# With text annotations ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot(data = mtcars) +
geom_starglyph(aes(x = mpg, y = disp, colour = cyl),
cols = zs, whisker = TRUE, contour = FALSE,
size = 10) +
geom_point(data = mtcars, aes(x = mpg, y = disp, colour = cyl)) +
geom_text(data = mtcars, aes(x = mpg, y = disp, label = lab), cex = 2) +
ylim(c(-0, 550))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Faceted ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot(data = mtcars) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 10, alpha = 0.5) +
ylim(c(-0, 550)) +
facet_grid(. ~ cyl)
ggplot(data = mtcars) +
geom_starglyph(aes(x = mpg, y = disp, colour = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 10) +
ylim(c(-0, 550)) +
facet_grid(. ~ cyl)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Repel glyphs ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot(data = mtcars) +
geom_point(aes(x = mpg, y = disp, colour = cyl)) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 10, alpha = 1, repel = TRUE) +
ylim(c(-0, 550)) +
xlim(c(8, 35))
#> Warning: 5 glyphs have too many overlaps.
#> Consider increasing "max.overlaps"
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# With grid points ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot(data = mtcars_fct) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 3, alpha = 0.5, draw.grid = TRUE,
grid.point.size = 5) +
ylim(c(-0, 550))
ggplot(data = mtcars_fct) +
geom_starglyph(aes(x = mpg, y = disp, colour = cyl),
cols = zs, whisker = TRUE, contour = FALSE,
size = 3, draw.grid = TRUE, grid.point.size = 7,
linewidth.whisker = 2, alpha = 0.7) +
ylim(c(-0, 550))
ggplot(data = mtcars_fct) +
geom_starglyph(aes(x = mpg, y = disp),
cols = zs, whisker = TRUE, contour = FALSE,
size = 3, draw.grid = TRUE,
grid.point.size = 5, alpha = 0.8,
colour.whisker = RColorBrewer::brewer.pal(8, "Dark2")) +
geom_point(data = mtcars_fct, aes(x = mpg, y = disp)) +
ylim(c(-0, 550))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Legend options ----
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Default legend/guide
ggplot(data = mtcars) +
geom_point(aes(x = mpg, y = disp, colour = cyl)) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 10, alpha = 1, repel = TRUE) +
ylim(c(-0, 550)) +
xlim(c(8, 35))
#> Warning: 5 glyphs have too many overlaps.
#> Consider increasing "max.overlaps"
# 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_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 5, alpha = 0.8, 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_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 10, alpha = 1, repel = TRUE,
legend.glyph.dims = zavg) +
ylim(c(-0, 550)) +
xlim(c(8, 35))
#> Warning: 5 glyphs have too many overlaps.
#> Consider increasing "max.overlaps"
ggplot(data = mtcars) +
geom_point(aes(x = mpg, y = disp, colour = cyl), show.legend = FALSE) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 5, alpha = 0.8, 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
# starglyphGrob
guide_stargrob <- starglyphGrob(
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 starglyphGrob
guide_stargrob <-
addlabel.glyphGrob(grob = guide_stargrob, label = zs,
push = 1, segment = FALSE)
# Another version
guide_stargrob2 <- starglyphGrob(
z = rep(0.5, length(zs)),
size = 25)
guide_stargrob2 <-
addlabel.glyphGrob(grob = guide_stargrob2, label = zs,
push = 1, segment = FALSE)
ggplot(data = mtcars) +
geom_point(aes(x = mpg, y = disp, colour = cyl), show.legend = FALSE) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 5, alpha = 0.8, repel = TRUE) +
ylim(c(-0, 550)) +
guides(fill = guide_legend(order = 1, position = "right"),
custom = guide_custom(guide_stargrob,
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_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 5, alpha = 0.8, repel = TRUE) +
ylim(c(-0, 550)) +
guides(fill = guide_legend(order = 1, position = "right"),
custom = guide_custom(guide_stargrob2,
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 points
z_grid <- c(hp = 3, drat = 3, wt = 2,
qsec = 2, vs = 3, am = 4,
gear = 2, carb = 3)
ggplot(data = mtcars_fct) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 1.5, alpha = 0.5, draw.grid = TRUE,
grid.point.size = 5) +
ylim(c(-0, 550))
ggplot(data = mtcars_fct) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 1.5, alpha = 0.5, draw.grid = TRUE,
grid.point.size = 5) +
ylim(c(-0, 550)) +
scale_z_discrete(z = zs) +
guide_z_order(z = zs, default_aes = "fill") +
legend_theme
ggplot(data = mtcars_fct) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 1.5, alpha = 0.5, draw.grid = TRUE,
grid.point.size = 5,
legend.glyph.dims = z_grid) +
ylim(c(-0, 550))
ggplot(data = mtcars_fct) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 2, alpha = 0.5, draw.grid = TRUE,
grid.point.size = 5,
legend.glyph.dims = z_grid) +
ylim(c(-0, 550)) +
scale_z_discrete(z = zs) +
guide_z_order(z = zs, default_aes = "fill") +
legend_theme
# starglyphGrob with grid levels
z_grid_levels <- lapply(z_grid, function(x) 1:x)
guide_stargrob_grid <-
starglyphGrob(z = z_grid,
size = 3, draw.grid = TRUE,
grid.levels = z_grid_levels,
grid.point.size = 0.1)
guide_stargrob_grid <-
addlabel.glyphGrob(grob = guide_stargrob_grid, label = zs,
push = 1, segment = FALSE)
# Another version
guide_stargrob_grid2 <-
starglyphGrob(z = rep(3, length(z_grid)),
size = 3, draw.grid = TRUE,
grid.levels = replicate(8, 1:3,
simplify = FALSE),
grid.point.size = 0.1)
guide_stargrob_grid2 <-
addlabel.glyphGrob(grob = guide_stargrob_grid2, label = zs,
push = 1, segment = FALSE)
ggplot(data = mtcars_fct) +
geom_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 2, alpha = 0.5, draw.grid = TRUE,
grid.point.size = 5,
legend.glyph.dims = z_grid) +
ylim(c(-0, 550)) +
guides(fill = guide_legend(order = 1, position = "right"),
custom = guide_custom(guide_stargrob_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_starglyph(aes(x = mpg, y = disp, fill = cyl),
cols = zs, whisker = TRUE, contour = TRUE,
size = 2, alpha = 0.5, draw.grid = TRUE,
grid.point.size = 5,
legend.glyph.dims = z_grid) +
ylim(c(-0, 550)) +
guides(fill = guide_legend(order = 1, position = "right"),
custom = guide_custom(guide_stargrob_grid2,
width = unit(0.1, "npc"),
height = unit(0.1, "npc"),
position = "bottom",
theme = theme(legend.margin = margin(t = 20, b = 30))))
# }