Learn R Programming

⚠️There's a newer version (0.3.0) of this package.Take me there.

ggh4x

The ggh4x package is a ggplot2 extension package. It provides some utility functions that don’t entirely fit within the ‘grammar of graphics’ concept —they can be a bit hacky— but can nonetheless be useful in tweaking your ggplots. Examples include adjusting the sizes of facets, mapping multiple aesthetics to colours and specifying individual scales for facets. Besides this, it is also a small collection of geoms, facets, positions, guides and stats.

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("teunbrand/ggh4x")

Overview

There are a few topics explored in the package’s vignettes with examples. Links to these topics are below.

Example

Below you’ll find an example that illustrates some of the features of ggh4x.

library(ggh4x)
#> Loading required package: ggplot2
#> Warning: package 'ggplot2' was built under R version 4.1.1
library(scales)

df <- transform(
  iris, 
  Nester = ifelse(Species == "setosa", "Short Leaves", "Long Leaves")
)

# Basic plot
g <- ggplot(df, aes(Sepal.Width, Sepal.Length)) +
  theme_classic() +
  theme(strip.background = element_blank())

# For making a plot with multiple colour scales, we'd first need to make layers
# with alternative aesthetics. We'll choose a colour scale for every species.
# This will produce a few warnings, as ggplot2 doesn't know how to deal with
# the alternative aesthetics.
g <- g + 
  geom_point(aes(SW = Sepal.Width),
             data = ~ subset(., Species == "setosa")) +
  geom_point(aes(PL = Petal.Length),
             data = ~ subset(., Species == "versicolor")) +
  geom_point(aes(PW = Petal.Width),
             data = ~ subset(., Species == "virginica"))
#> Warning: Ignoring unknown aesthetics: SW
#> Warning: Ignoring unknown aesthetics: PL
#> Warning: Ignoring unknown aesthetics: PW

# These alternative aesthetics don't mean a lot until we add a multi-colour
# scale to the plot. We need to specify our alternative aesthetics and colours
# for every scale. Arguments provided as lists are passed on to individual 
# scales.
g <- g +
  scale_colour_multi(
    aesthetics = c("SW", "PL", "PW"),
    name = list("Blue", "Pink", "Orange"),
    colours = list(
      brewer_pal(palette = "YlGnBu")(6),
      brewer_pal(palette = "RdPu")(6),
      brewer_pal(palette = "YlOrRd")(6)
    ),
    guide = guide_colorbar(barheight = unit(50, "pt"))
  )
g
# We can make a facet wherein duplicated strip labels are merged into one strip
g <- g + 
  facet_nested(~ Nester + Species, scales = "free",
               nest_line = TRUE)

# Like we did for colours, we might also want to set position scales for every
# panel individually. We set these in the same order the facets appear in.
position_scales <- list(
  scale_x_reverse(guide = "axis_minor"),
  scale_x_continuous(labels = dollar, guide = "axis_truncated"),
  scale_x_continuous(breaks = c(3, 4), expand = c(0,0))
)

# Adding the list of scales to the plot
g <- g + facetted_pos_scales(x = position_scales)

# Setting the sizes of panels individually
size <- 2 / (1 + sqrt(5))
g <- g + force_panelsizes(cols = c(1, size, size ^ 2), respect = TRUE)
g

Footnote

I would like to mention that there are also packages that do some similar things to what this package does. facetscales also has a facet function wherein scales can set per row/column. The egg package can also set panel sizes. The lemon package also has options to tweak position axes. The relayer and ggnewscale packages also allow multiple colour scales in the same plot.

Historically, many of these functions come from the ggnomics package, but have been moved here as a package independent of Bioconductor infrastructure.

Copy Link

Version

Install

install.packages('ggh4x')

Monthly Downloads

23,567

Version

0.2.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Teun van den Brand

Last Published

November 24th, 2021

Functions in ggh4x (0.2.1)

facet_grid2

Extended grid facets
facet_manual

Manual layout for panels
facet_wrap2

Extended wrapped facets
force_panelsizes

Force a facetted plot to have specified panel sizes
element_part_rect

Partial rectangle theme element
facet_nested

Layout panels in a grid with nested strips
facetted_pos_scales

Set individual scales in facets
center_limits

Center limits
facet_nested_wrap

Ribbon of panels with nested strips.
distribute_args

Element list constructors
geom_pointpath

Point Paths
FacetGrid2

ggh4x extensions to ggplot2
ggh4x-package

ggh4x: Hacks for 'ggplot2'
guide_axis_manual

Manual axis
guide_axis_minor

Axis guide with ticks for minor breaks
scale_dendrogram

Dendrogram position scales
position_lineartrans

Linearly transform coordinates
geom_polygonraster

Polygon parameterisation for rasters
position_disjoint_ranges

Segregating overlapping ranges
geom_text_aimed

Aimed text
geom_rectmargin

Rectangular rugs in the margins
help_secondary

Secondary axis helper
ggsubset

Passing a subset of data to ggplot2 layers.
guide_dendro

Dendrogram guide
guide_axis_logticks

Axis guide with ticks for logarithmic breaks
guide_axis_truncated

Axis guide with truncated line
guide_axis_nested

Nested axis guide
scale_listed

Add a list of scales for non-standard aesthetics
scale_fill_multi

Multiple gradient colour scales
guide_stringlegend

String legend
stat_rle

Run length encoding
strip_nested

Nested strips
stat_theodensity

Fitted theoretical density
weave_factors

Bind together factors
stat_funxy

Apply function to position coordinates
stat_difference

Difference ribbon
stat_rollingkernel

Rolling Kernel
strip_vanilla

Default strips
strip_themed

Strip with themed boxes and texts