MyNixOS website logo
Description

Streamlined Creation of Segments on the Y-Axis of 'ggplot2' Plots.

The function gggap() streamlines the creation of segments on the y-axis of 'ggplot2' plots which is otherwise not a trivial task to accomplish.

gggap

Streamlined creation of segments on the y-axis of 'ggplot2' plots

This project builds on top of gg.gap v1.4 by Jiacheng Lou, et al.

What changed with respect to upstream?

  • Improved handling of subtitles and captions. These were duplicated and placed in the gap between the new segments of the plot.
  • Legends are now supported by the core functionality.
  • The code was heavily refactored following the tidyverse style guide and Clean Code principles.
  • The project has a new name after lossing the punctuation mark from the former name as per the recommendations of the style guide.

Future work:

  • Support the creation of gaps on the x-axis
  • Introduce testing
  • Revise the examples provided

Installation

You can install gggap from CRAN:

install.packages("gggap")

A development version can be retrieved from Github:

# install.packages("devtools")
devtools::install_github("cmoralesmx/gggap", ref="dev")

How to use gggap

data(mtcars)
library(ggplot2)
p <- ggplot(data = mtcars, aes(x = gear, fill = gear)) +
  geom_bar() +
  ggtitle("Number of Cars by Gears") +
  xlab("Gears")

# single segments and missing `tick_width`
gggap(plot = p, segments = c(5, 10), ylim = c(0, 50))

# `tick_width` can be one or more numbers
gggap(
  plot = p,
  segments = c(5, 10),
  tick_width = c(1, 10),
  ylim = c(0, 50)
)

# segments list cantains more than one number vectors
gggap(
  plot = p,
  segments = list(c(2.5, 4),c(5, 10)),
  tick_width = c(1, 0.5, 10),
  ylim = c(0, 50))

# `rel_heights` can set the relative height for segments and segmented y-axis
gggap(
  plot = p,
  segments = list(c(2.5, 4),c(5, 10)),
  tick_width = c(1, 0.5, 10),
  rel_heights = c(0.2, 0, 0.2, 0,1),
  ylim = c(0, 50)
)

# reversed y-axis
p <- ggplot(
  data = mtcars,
  aes(x = gear, fill = gear)
) +
geom_bar() +
ggtitle("Number of Cars by Gears") +
xlab("Gears")+
scale_y_continuous(trans = 'reverse')
# single segments and missing tick_width
gggap(plot = p, segments = c(10, 5), ylim = c(15, 0))

# for facet()
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, hp)) + geom_point()
p1 <- p + facet_wrap(~cyl, scales = "free")
gggap(plot = p1, ylim = c(60, 200), segments = c(100, 120))
Metadata

Version

1.0.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows