Horizontal 'ggplot2' Components

A 'ggplot2' extension that provides flipped components: horizontal versions of 'Stats' and 'Geoms', and vertical versions of 'Positions'.

ggstance implements horizontal versions of common ggplot2 Geoms, Stats, and Positions.


Get the development version from Github with:

# install.packages("devtools")

Horizontal geoms

While coord_flip() can only flip a plot as a whole, ggstance provides flipped versions of Geoms, Stats and Positions. This makes it easier to build horizontal layer or use vertical positioning (e.g. vertical dodging). Also, horizontal Geoms draw horizontal legend keys to keep the appearance of your plots consistent.

Horizontal Geoms:

  • geom_barh()
  • geom_histogramh()
  • geom_linerangeh()
  • geom_pointrangeh()
  • geom_errorbarh()
  • geom_crossbarh()
  • geom_boxploth()
  • geom_violinh()

Horizontal Stats:

  • stat_binh()
  • stat_boxploth()
  • stat_counth()
  • stat_xdensity()
  • stat_summaryh()

Vertical Positions:

  • position_dodgev
  • position_nudgev
  • position_fillv
  • position_stackv
  • position_jitterdodgev



To create a horizontal layer in ggplot2 with coord_flip(), you have to supply aesthetics as if they were to be drawn vertically:

# Vertical
ggplot(mpg, aes(class, hwy, fill = factor(cyl))) +
# Horizontal with coord_flip()
ggplot(mpg, aes(class, hwy, fill = factor(cyl))) +
  geom_boxplot() +

In ggstance, you supply aesthetics in their natural order:

# Horizontal with ggstance
ggplot(mpg, aes(hwy, class, fill = factor(cyl))) +

Horizontal boxplot

Facetting with Free Scales

Some plots are hard to produce with coord_flip(). One case is facetting with free scales. Here is an example from @smouksassi:

df <- data.frame(
  Group = factor(rep(1:3, each = 4), labels = c("Drug A", "Drug B", "Control")),
  Subject = factor(rep(1:6, each = 2), labels = c("A", "B", "C", "D", "E", "F")),
  Result = rnorm(12)
vertical <- ggplot(df, aes(Subject, Result))+
  geom_boxplot(aes(fill = Group))+
  facet_grid(. ~ Group, scales = "free_x")

How do we flip this plot? With coord_flip(), the free scales are not flipped correctly:

vertical + coord_flip()
vertical + facet_grid(Group ~ ., scales = "free_x") + coord_flip()

On the other hand a ggstance horizontal layer will work properly:

horizontal <- ggplot(df, aes(Result, Subject))+
  geom_boxploth(aes(fill = Group))+
  facet_grid(Group ~ ., scales = "free_y")

Horizontal free-scales facetting

Using vertical positions

In this example we use vertical dodging to align measurements within subgroups.

data <- expand.grid(
  Group = c("A", "B"),
  Subgroup = c("a", "b", "c"),
  y = 1:10
data$y <- sample(1:4, replace = TRUE, size = nrow(data))
ggplot(data, aes(y, Group, colour = Subgroup)) +
  stat_sum(position = position_dodgev(height = 0.5))

Vertical positions


ggstance 0.3.1

This version updates all ggstance layers to feature parity with ggplot2 3.0.0.

  • New horizontal geom_colh() layer.

  • stat_counth() is now exported (#22).

  • position_stackv() gains hjust and reverse arguments (#17).

  • New horizontal summary functions mean_se_h(), mean_cl_boot_h(), mean_cl_normal_h(), mean_sdl_h() and median_hilow_h() (#13, thanks to @rjbgoudie).

  • Change minimal R version to 3.1.0, to be consistent with ggplot2.

In addition a few bugs have been fixed:

  • geom_barh() now uses the counth statistic by default instead of count. This fixes many issues with this layer (#19, thanks to @erocoar).

  • geom_violinh() can now be passed draw_quantiles argument (#16, thanks to @iamamutt).

  • Fix horizontal boxplot issue when y scale is continuous.

ggstance 0.3.0

The package was rewritten to make it more robust to ggplot2 updates. In addition, this version features:

  • New stat_summaryh() (#7)

ggstance 0.2.0

Major change

ggstance has been reorganised to make it more modular. Before, it would create horizontal layer by automatically flipping Geoms and Stats. Now it creates regular ggplot2 layer to which you provide horizontal Geoms, Stats, and vertical Positions. This allows you to reuse some of these components in regular layers. Vertical positions in particular can be useful.

Bug fixes

  • Fix error messages when required aesthetics are not provided (#5)

ggstance 0.1.0

Initial release

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.3.1 by Lionel Henry, a year ago

Browse source code at https://github.com/cran/ggstance

Authors: Lionel Henry [aut, cre] , Hadley Wickham [aut] , Winston Chang [aut] , RStudio [cph]

Documentation:   PDF Manual  

GPL-3 license

Imports ggplot2, plyr, rlang, withr

Suggests Hmisc, testthat, vdiffr

Imported by cobalt, coveffectsplot, cregg, dotwhisker, ggquickeda, scatr, tidybayes.

Depended on by ggformula.

Suggested by interactions, jtools.

See at CRAN