ggside R Package

repository·main·Indexed 18 days ago

https://github.com/jtlandis/ggside

An R package that extends ggplot2 to allow the addition of side panels to plots. These panels can display summary statistics, such as density or boxplots, or metadata relative to the main panel's axes. It provides specialized geometries following the geom_xside* and geom_yside* naming patterns and includes theme elements to customize panel scale, borders, grids, and backgrounds.

Tokens
2.1K
Snippets
7
Records
9
Agent score
13%

What's inside ggside

  1. How ggside works with ggplot2

    main

    The ggside package extends ggplot2 by allowing you to add side panels that display graphical information relative to the main panel's axes. This is useful for showing metadata for discrete axes or summary statistics (like boxplots or density distributions) for continuous axes.

    Geometries follow a naming pattern: geom_xside* adds a layer to the x-side panel, and geom_yside* adds a layer to the y-side panel. By default, an xside geometry is positioned above the main panel and shares the same x-axis but has an independent y-axis.

    library(ggplot2)
    library(ggside)
    
    ggplot(mpg, aes(displ, hwy, colour = class)) +
      geom_point(size = 2) +
      geom_xsidedensity(aes(y = after_stat(density)), position = "stack") +
      geom_ysidedensity(aes(x = after_stat(density)), position = "stack") +
      theme(axis.text.x = element_text(angle = 90, vjust = .5))
  2. Mix discrete and continuous axes in ggside

    main

    Since version 0.3.0, you can mix discrete and continuous axes without manually using scale_(y|x)side(x|y)_*() functions. ggside geometries automatically handle the mapping of positional aesthetics (x and y) to ysidex and xsidey under the hood.

    ggplot(mpg, aes(displ, hwy, colour = class)) +
      geom_point(size = 2) +
      geom_xsideboxplot(aes(y =class), orientation = "y") +
      geom_ysidedensity(aes(x = after_stat(density)), position = "stack") +
      scale_ysidex_continuous(guide = guide_axis(angle = 90), minor_breaks = NULL) +
      theme(ggside.panel.scale = .3)
  3. Install ggside

    main

    You can install the latest stable version of ggside from CRAN or install the development version directly from GitHub using devtools.

    #CRAN
    utils::install.packages("ggside")
    #Github
    devtools::install_github("jtlandis/ggside")
  4. Overview of ggside

    main
    The ggside package extends ggplot2 by allowing users to add graphical information (side panels) to the axes of a main plot panel. This is useful for displaying metadata for discrete axes or summary statistics (like boxplots or density distributions) for continuous axes.
  5. How to add side panels using ggside geometries

    main

    Adding side panels is similar to adding standard ggplot2 layers. Geometries follow a naming pattern: geom_xside* adds a geometry to the x-side panel, and geom_yside* adds a geometry to the y-side panel.

    By default, an xside geometry (like geom_xsidedensity) is positioned above the main panel and shares the main panel's x-axis but maintains an independent y-axis.

    library(ggplot2)
    library(ggside)
    
    ggplot(mpg, aes(displ, hwy, colour = class)) +
      geom_point(size = 2) +
      geom_xsidedensity(aes(y = after_stat(density)), position = "stack") +
      geom_ysidedensity(aes(x = after_stat(density)), position = "stack") +
      theme(axis.text.x = element_text(angle = 90, vjust = .5))
  6. Mixing discrete and continuous axes with ggside

    main

    Since version 0.3.0, you can mix discrete and continuous axes without manually using scale_(y|x)side(x|y)_*() functions. ggside geometries automatically handle the necessary aesthetic mapping (ysidex and xsidey) under the hood. You can control the side panel size using theme(ggside.panel.scale = .3).

    ggplot(mpg, aes(displ, hwy, colour = class)) +
      geom_point(size = 2) +
      geom_xsideboxplot(aes(y = class), orientation = "y") +
      geom_ysidedensity(aes(x = after_stat(density)), position = "stack") +
      scale_ysidex_continuous(guide = guide_axis(angle = 90), minor_breaks = NULL) +
      theme(ggside.panel.scale = .3)
  7. Fix missing data when mixing positional scales

    main

    A known issue occurs when a layer requires a positional scale on the main panel to be computed later, but the same scale is present on a parallel side layer meant to be discrete. This can result in warnings and missing data.

    Problematic pattern:

    ggplot(iris, aes(Sepal.Width)) +
      geom_density() +
      geom_xsidepoint(aes(y = Species))

    Solution: Explicitly declare the scale for the main panels to ensure proper initialization.

    ggplot(iris, aes(Sepal.Width)) +
      geom_density() +
      geom_xsidepoint(aes(y = Species)) +
      scale_y_continuous()
  8. Customize ggside panel themes

    main

    Starting from version 0.2.0, you can use theme() to control the appearance of side panels. Key theme elements include:

    • ggside.panel.scale: Controls the scale of the side panels.
    • ggside.panel.border: Controls the border of the side panels (e.g., element_rect()).
    • ggside.panel.grid: Controls the grid lines within side panels (e.g., element_line()).
    • ggside.panel.background: Controls the background of the side panels (e.g., element_blank()).
    ggplot(iris, aes(Sepal.Width, Sepal.Length, fill = Species)) +
      geom_point(aes(color = Species)) +
      geom_xsidedensity(alpha = .3, position = "stack") +
      geom_ysideboxplot(aes(x = Species), orientation = "x") +
      scale_ysidex_discrete(guide = guide_axis(angle = 45)) +
      theme_dark() +
      theme(ggside.panel.scale = .3,
            ggside.panel.border = element_rect(NA, "red", linewidth = 2),
            ggside.panel.grid = element_line("black", linewidth = .1, linetype = "dotted"),
            ggside.panel.background = element_blank()) +
      guides(color = "none", fill = "none")
  9. Customize side panel appearance with theme elements

    main

    You can control the visual rendering of side panels using specific ggside theme elements. Key elements include:

    • ggside.panel.scale: Controls the relative scale of the side panels.
    • ggside.panel.border: Controls the border of the side panels.
    • ggside.panel.grid: Controls the grid lines within the side panels.
    • ggside.panel.background: Controls the background of the side panels.
    ggplot(iris, aes(Sepal.Width, Sepal.Length, fill = Species)) +
      geom_point(aes(color = Species)) +
      geom_xsidedensity(alpha = .3, position = "stack") +
      geom_ysideboxplot(aes(x = Species), orientation = "x") +
      scale_ysidex_discrete(guide = guide_axis(angle = 45)) +
      theme_dark() +
      theme(ggside.panel.scale = .3,
            ggside.panel.border = element_rect(NA, "red", linewidth = 2),
            ggside.panel.grid = element_line("black", linewidth = .1, linetype = "dotted"),
            ggside.panel.background = element_blank()) +
      guides(color = "none", fill = "none")