
ggplot2 geoms with support for pattern fills
Source:R/a-geom-docs.R, R/geom-rect.R, R/geom-bar.R, and 17 more
geom-docs.RdAll geoms in this package are identical to their counterparts in ggplot2 except that they can be filled with patterns.
Usage
geom_rect_pattern(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
lineend = "butt",
linejoin = "mitre",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_bar_pattern(
mapping = NULL,
data = NULL,
stat = "count",
position = "stack",
...,
just = 0.5,
lineend = "butt",
linejoin = "mitre",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_tile_pattern(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
lineend = "butt",
linejoin = "mitre",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_bin_2d_pattern(
mapping = NULL,
data = NULL,
stat = "bin2d",
position = "identity",
...,
lineend = "butt",
linejoin = "mitre",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_bin2d_pattern(
mapping = NULL,
data = NULL,
stat = "bin2d",
position = "identity",
...,
lineend = "butt",
linejoin = "mitre",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_boxplot_pattern(
mapping = NULL,
data = NULL,
stat = "boxplot",
position = "dodge2",
...,
outliers = TRUE,
outlier.colour = NULL,
outlier.color = NULL,
outlier.fill = NULL,
outlier.shape = NULL,
outlier.size = NULL,
outlier.stroke = 0.5,
outlier.alpha = NULL,
whisker.colour = NULL,
whisker.color = NULL,
whisker.linetype = NULL,
whisker.linewidth = NULL,
staple.colour = NULL,
staple.color = NULL,
staple.linetype = NULL,
staple.linewidth = NULL,
median.colour = NULL,
median.color = NULL,
median.linetype = NULL,
median.linewidth = NULL,
box.colour = NULL,
box.color = NULL,
box.linetype = NULL,
box.linewidth = NULL,
notch = FALSE,
notchwidth = 0.5,
staplewidth = 0,
varwidth = FALSE,
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE
)
geom_col_pattern(
mapping = NULL,
data = NULL,
stat = "identity",
position = "stack",
...,
just = 0.5,
lineend = "butt",
linejoin = "mitre",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_polygon_pattern(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
rule = "evenodd",
lineend = "butt",
linejoin = "round",
linemitre = 10,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_contour_filled_pattern(
mapping = NULL,
data = NULL,
stat = "contour_filled",
position = "identity",
...,
bins = NULL,
binwidth = NULL,
breaks = NULL,
rule = "evenodd",
lineend = "butt",
linejoin = "round",
linemitre = 10,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_crossbar_pattern(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
middle.colour = NULL,
middle.color = NULL,
middle.linetype = NULL,
middle.linewidth = NULL,
box.colour = NULL,
box.color = NULL,
box.linetype = NULL,
box.linewidth = NULL,
fatten = deprecated(),
na.rm = FALSE,
orientation = NA,
show.legend = NA,
inherit.aes = TRUE
)
geom_ribbon_pattern(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
orientation = NA,
lineend = "butt",
linejoin = "round",
linemitre = 10,
outline.type = "both",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_area_pattern(
mapping = NULL,
data = NULL,
stat = "align",
position = "stack",
...,
orientation = NA,
outline.type = "upper",
lineend = "butt",
linejoin = "round",
linemitre = 10,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_density_pattern(
mapping = NULL,
data = NULL,
stat = "density",
position = "identity",
...,
outline.type = "upper",
lineend = "butt",
linejoin = "round",
linemitre = 10,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_density_2d_filled_pattern(
mapping = NULL,
data = NULL,
stat = "density_2d_filled",
position = "identity",
...,
contour_var = "density",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_density2d_filled_pattern(
mapping = NULL,
data = NULL,
stat = "density_2d_filled",
position = "identity",
...,
contour_var = "density",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_dotplot_pattern(
mapping = NULL,
data = NULL,
position = "identity",
...,
binwidth = NULL,
binaxis = "x",
method = "dotdensity",
binpositions = "bygroup",
stackdir = "up",
stackratio = 1,
dotsize = 1,
stackgroups = FALSE,
origin = NULL,
right = TRUE,
width = 0.9,
drop = FALSE,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_hex_pattern(
mapping = NULL,
data = NULL,
stat = "binhex",
position = "identity",
...,
lineend = "butt",
linejoin = "mitre",
linemitre = 10,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_histogram_pattern(
mapping = NULL,
data = NULL,
stat = "bin",
position = "stack",
...,
binwidth = NULL,
bins = NULL,
orientation = NA,
lineend = "butt",
linejoin = "mitre",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_map_pattern(
mapping = NULL,
data = NULL,
stat = "identity",
...,
map,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_point_pattern(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_sf_pattern(
mapping = aes(),
data = NULL,
stat = "sf",
position = "identity",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
...
)
geom_violin_pattern(
mapping = NULL,
data = NULL,
stat = "ydensity",
position = "dodge",
...,
trim = TRUE,
bounds = c(-Inf, Inf),
quantile.colour = NULL,
quantile.color = NULL,
quantile.linetype = 0L,
quantile.linewidth = NULL,
draw_quantiles = deprecated(),
scale = "area",
na.rm = FALSE,
orientation = NA,
show.legend = NA,
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()'sparamsargument. These arguments broadly fall into one of 4 categories below. Notably, further arguments to thepositionargument, or aesthetics that are required can not be passed through.... Unknown arguments that are not part of the 4 categories below are ignored.Static aesthetics that are not mapped to a scale, but are at a fixed value and apply to the layer as a whole. For example,
colour = "red"orlinewidth = 3. The geom's documentation has an Aesthetics section that lists the available options. The 'required' aesthetics cannot be passed on to theparams. Please note that while passing unmapped aesthetics as vectors is technically possible, the order and required length is not guaranteed to be parallel to the input data.When constructing a layer using a
stat_*()function, the...argument can be used to pass on parameters to thegeompart of the layer. An example of this isstat_density(geom = "area", outline.type = "both"). The geom's documentation lists which parameters it can accept.Inversely, when constructing a layer using a
geom_*()function, the...argument can be used to pass on parameters to thestatpart of the layer. An example of this isgeom_area(stat = "density", adjust = 0.5). The stat's documentation lists which parameters it can accept.The
key_glyphargument oflayer()may also be passed on through.... This can be one of the functions described as key glyphs, to change the display of the layer in the legend.
- lineend
Line end style (round, butt, square).
- linejoin
Line join style (round, mitre, bevel).
- na.rm
If
FALSE, the default, missing values are removed with a warning. IfTRUE, missing values are silently removed.- 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.- 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().- just
Adjustment for column placement. Set to
0.5by default, meaning that columns will be centered about axis breaks. Set to0or1to place columns to the left/right of axis breaks. Note that this argument may have unintended behaviour when used with alternative positions, e.g.position_dodge().- outliers
Whether to display (
TRUE) or discard (FALSE) outliers from the plot. Hiding or discarding outliers can be useful when, for example, raw data points need to be displayed on top of the boxplot. By discarding outliers, the axis limits will adapt to the box and whiskers only, not the full data range. If outliers need to be hidden and the axes needs to show the full data range, please useoutlier.shape = NAinstead.- outlier.colour, outlier.color, outlier.fill, outlier.shape, outlier.size, outlier.stroke, outlier.alpha
Default aesthetics for outliers. Set to
NULLto inherit from the data's aesthetics.- whisker.colour, whisker.color, whisker.linetype, whisker.linewidth
Default aesthetics for the whiskers. Set to
NULLto inherit from the data's aesthetics.- staple.colour, staple.color, staple.linetype, staple.linewidth
Default aesthetics for the staples. Set to
NULLto inherit from the data's aesthetics. Note that staples don't appear unless thestaplewidthargument is set to a non-zero size.- median.colour, median.color, median.linetype, median.linewidth
Default aesthetics for the median line. Set to
NULLto inherit from the data's aesthetics.- box.colour, box.color, box.linetype, box.linewidth
Default aesthetics for the boxes. Set to
NULLto inherit from the data's aesthetics.- notch
If
FALSE(default) make a standard box plot. IfTRUE, make a notched box plot. Notches are used to compare groups; if the notches of two boxes do not overlap, this suggests that the medians are significantly different.- notchwidth
For a notched box plot, width of the notch relative to the body (defaults to
notchwidth = 0.5).- staplewidth
The relative width of staples to the width of the box. Staples mark the ends of the whiskers with a line.
- varwidth
If
FALSE(default) make a standard box plot. IfTRUE, boxes are drawn with widths proportional to the square-roots of the number of observations in the groups (possibly weighted, using theweightaesthetic).- orientation
The orientation of the layer. The default (
NA) automatically determines the orientation from the aesthetic mapping. In the rare event that this fails it can be given explicitly by settingorientationto either"x"or"y". See the Orientation section for more detail.- rule
Either
"evenodd"or"winding". If polygons with holes are being drawn (using thesubgroupaesthetic) this argument defines how the hole coordinates are interpreted. See the examples ingrid::pathGrob()for an explanation.- linemitre
Line mitre limit (number greater than 1).
- bins
For
geom_histogram_pattern()the number of bins (overridden bybinwidth, defaults to 30) and forgeom_contour_filled_pattern()the number of contour bins (overridden bybreaks).- binwidth
For
geom_histogram_pattern()the width of the bins (seeggplot2::geom_histogram()), forgeom_dotplot_pattern()the maximum bin width whenmethodis"dotdensity"or else the bin width (seeggplot2::geom_dotplot()), and forgeom_contour_filled_pattern()the width of the contour bins (overridden bybins).- breaks
For
geom_contour_filled_pattern()the contour breaks: a numeric vector or a function of the range of the data and thebinwidthreturning them (seeggplot2::geom_contour_filled()). Overridesbinwidthandbins.- middle.colour, middle.color, middle.linetype, middle.linewidth
Default aesthetics for the middle line. Set to
NULLto inherit from the data's aesthetics.- fatten
A multiplicative factor used to increase the size of the middle bar in
geom_crossbar()and the middle point ingeom_pointrange().- outline.type
Type of the outline of the area;
"both"draws both the upper and lower lines,"upper"/"lower"draws the respective lines only."full"draws a closed polygon around the area.- contour_var
Character string identifying the variable to contour by. Can be one of
"density","ndensity", or"count". See the section on computed variables for details.- binaxis
The axis to bin along, "x" (default) or "y"
- method
"dotdensity" (default) for dot-density binning, or "histodot" for fixed bin widths (like stat_bin)
- binpositions
When
methodis "dotdensity", "bygroup" (default) determines positions of the bins for each group separately. "all" determines positions of the bins with all the data taken together; this is used for aligning dot stacks across multiple groups.- stackdir
which direction to stack the dots. "up" (default), "down", "center", "centerwhole" (centered, but with dots aligned)
- stackratio
how close to stack the dots. Default is 1, where dots just touch. Use smaller values for closer, overlapping dots.
- dotsize
The diameter of the dots relative to
binwidth, default 1.- stackgroups
should dots be stacked across groups? This has the effect that
position = "stack"should have, but can't (because this geom has some odd properties).- origin
When
methodis "histodot", origin of first bin- right
When
methodis "histodot", should intervals be closed on the right (a, b], or not [a, b)- width
When
binaxisis "y", the spacing of the dot stacks for dodging.- drop
Treatment of zero count bins. If
"none"(default), such bins are kept as-is. If"all", all zero count bins are filtered out. If"extremes"only zero count bins at the flanks are filtered out, but not in the middle.TRUEis shorthand for"all"andFALSEis shorthand for"none".- map
Data frame that contains the map coordinates. This will typically be created using
fortify()on a spatial object. It must contain columnsxorlong,yorlat, andregionorid.- trim
If
TRUE(default), trim the tails of the violins to the range of the data. IfFALSE, don't trim the tails.- bounds
Known lower and upper bounds for estimated data. Default
c(-Inf, Inf)means that there are no (finite) bounds. If any bound is finite, boundary effect of default density estimation will be corrected by reflecting tails outsideboundsaround their closest edge. Data points outside of bounds are removed with a warning.- quantile.colour, quantile.color, quantile.linewidth, quantile.linetype
Default aesthetics for the quantile lines. Set to
NULLto inherit from the data's aesthetics. By default, quantile lines are hidden and can be turned on by changingquantile.linetype. Quantile values can be set using thequantilesargument when usingstat = "ydensity"(default).- draw_quantiles
- scale
if "area" (default), all violins have the same area (before trimming the tails). If "count", areas are scaled proportionally to the number of observations. If "width", all violins have the same maximum width.
Value
A ggplot2::Geom object.
Details
geom_point_pattern() fills each point with a filled symbol (shape 21 to 25, its default is 21)
with its own pattern (e.g. a different image per point) and requires R (>= 4.2.0).
Points with other shapes are drawn as by ggplot2::geom_point().
Pattern lengths (e.g. pattern_spacing) are relative to the plot panel by default
so for finer geometry patterns within (small) points use an absolute unit
e.g. pattern_units = "mm" with a small pattern_spacing.
geom_dotplot_pattern() fills each dot with its own pattern and requires R (>= 4.2.0).
Like ggplot2::geom_dotplot() its default fill is the theme's ink colour.
geom_hex_pattern() requires the suggested package {hexbin}.
Pattern Arguments
Not all arguments apply to all patterns.
Most pattern aesthetics default to the pattern's own default
(see the documentation of the corresponding gridpattern::grid.pattern_<name>() function)
which for most patterns is {gridpattern}'s generic default (listed below as "generic").
Internally these unset aesthetics have the value NaN, which {gridpattern} treats as unset
(whereas NA may be meaningful e.g. a transparent colour or a "pch" shape that draws nothing).
patternPattern name string e.g. 'stripe' (default), 'crosshatch', 'point', 'circle', 'none'
pattern_alphaAlpha transparency for pattern. default: pattern default (generic:
NAi.e. keep the colours' alpha)pattern_angleOrientation of the pattern in degrees. default: pattern default (generic: 30)
pattern_aspect_ratioAspect ratio adjustment. default: the aspect ratio of the plot panel / legend key
pattern_colourColour used for strokes and points. default: pattern default (generic: 'grey20')
pattern_densityApproximate fill fraction of the pattern. Usually in range [0, 1], but can be higher. default: pattern default (generic: 0.2)
pattern_filenameImage filename/URL. default: pattern default (generic: ”)
pattern_fillFill colour (or
grid::pattern()/gradient fill). default: pattern default (generic: 'grey80')pattern_fill2Second fill colour (or
grid::pattern()/gradient fill). default: pattern default (generic: '#4169E1')pattern_filter(Image scaling) filter. default: pattern default (generic: 'lanczos')
pattern_frequencyFrequency. default: pattern default (generic: 0.1)
pattern_gravityImage placement. default: pattern default (generic: 'center')
pattern_gridPattern grid type. default: pattern default (generic: 'square')
pattern_key_scale_factorZoom factor for the pattern in the legend key (multiplies the size of relative i.e. 'snpc' lengths relative to the key as well as the spacing and offsets of absolute lengths). default: 1
pattern_linetypeStroke linetype. default: pattern default (generic: 1)
pattern_linewidthStroke line width. default: pattern default (generic:
pattern_sizeif set else 0.5)pattern_option_1Generic user value for custom patterns. default: pattern default (generic: 0)
pattern_option_2Generic user value for custom patterns. default: pattern default (generic: 0)
pattern_option_3Generic user value for custom patterns. default: pattern default (generic: 0)
pattern_option_4Generic user value for custom patterns. default: pattern default (generic: 0)
pattern_option_5Generic user value for custom patterns. default: pattern default (generic: 0)
pattern_orientation'vertical', 'horizontal', or 'radial'. default: pattern default (generic: 'vertical')
pattern_phasePhase (generic user value for custom patterns). default: pattern default (generic: 0)
pattern_resPattern resolution (pixels per inch). default:
getOption("ggpattern_res")if set else slightly above the device resolution (72 ppi for vector devices such as PDF/SVG); legend keys use a higher resolution so that pixel-based patterns look like they do in the plotpattern_rotRotation angle (shape within pattern). default: pattern default (generic: 0)
pattern_scaleScale. default: pattern default (generic: 1)
pattern_shapePlotting shape. default: pattern default (generic: 1)
pattern_sizeFontsize ("text" pattern) or legacy stroke line width (superseded by
pattern_linewidth). default: pattern default (generic: 0.5)pattern_spacingSpacing of the pattern as a fraction of the plot size. default: pattern default (generic: 0.05)
pattern_typeGeneric control option. default: pattern default (generic:
NA)pattern_subtypeGeneric control option. default: pattern default (generic:
NA)pattern_xoffsetOffset the origin of the pattern. Range [0, 1]. default: pattern default (generic: 0). Use this to slightly shift the origin of the pattern. For most patterns, the user should limit the offset value to be less than the pattern spacing.
pattern_yoffsetOffset the origin of the pattern. Range [0, 1]. default: pattern default (generic: 0). Use this to slightly shift the origin of the pattern. For most patterns, the user should limit the offset value to be less than the pattern spacing.
Examples
if (require("ggplot2")) {
# 'stripe' pattern example
df <- data.frame(level = c("a", "b", "c", 'd'), outcome = c(2.3, 1.9, 3.2, 1))
gg <- ggplot(df) +
geom_col_pattern(
aes(level, outcome, pattern_fill = level),
pattern = 'stripe',
fill = 'white',
colour = 'black'
) +
theme_bw(18) +
theme(legend.position = 'none') +
labs(
title = "ggpattern::geom_col_pattern()",
subtitle = "pattern = 'stripe'"
)
plot(gg)
# 'pch' pattern example
gg <- ggplot(mtcars, aes(as.factor(cyl), mpg)) +
geom_violin_pattern(aes(fill = as.factor(cyl),
pattern_shape = as.factor(cyl)),
pattern = 'pch',
pattern_density = 0.3,
pattern_angle = 0,
colour = 'black'
) +
theme_bw(18) +
theme(legend.position = 'none') +
labs(
title = "ggpattern::geom_violin_pattern()",
subtitle = "pattern = 'pch'"
)
plot(gg)
# 'polygon_tiling' pattern example
gg <- ggplot(mtcars) +
geom_density_pattern(
aes(
x = mpg,
pattern_fill = as.factor(cyl),
pattern_type = as.factor(cyl)
),
pattern = 'polygon_tiling',
pattern_key_scale_factor = 1.2
) +
scale_pattern_type_manual(values = c("hexagonal", "rhombille",
"pythagorean")) +
theme_bw(18) +
theme(legend.key.size = unit(2, 'cm')) +
labs(
title = "ggpattern::geom_density_pattern()",
subtitle = "pattern = 'polygon_tiling'"
)
plot(gg)
}