tinyplot

repository·main·Indexed 19 days ago

https://github.com/grantmcdermott/tinyplot

A lightweight extension of the base R graphics system that provides convenience features such as automatic grouping, legends, facets, and themes. It maintains a minimal footprint with zero recursive dependencies and supports formula-based interfaces (y ~ x) via the tinyplot() and plt() functions.

Tokens
926
Snippets
7
Records
9
Agent score
15%

What's inside tinyplot

  1. Overview of tinyplot

    main

    tinyplot is a lightweight extension of the base R graphics system. It is designed to bridge the gap between the powerful infrastructure of base R (like the plot() generic and formula-based interfaces) and the convenience features found in grid-based packages like ggplot2 or lattice.

    Key features include:

    • Preservation of the base R formula-based interface (y ~ x).
    • Support for grouped plots with automatic legends and facets.
    • Advanced visualization types.
    • Easy customization via ready-made themes.
    • Minimal non-base dependencies.
  2. Install tinyplot

    main

    You can install the stable version of tinyplot from CRAN or the latest development version from R-universe.

    From CRAN:

    install.packages("tinyplot")

    From R-universe (development version):

    install.packages("tinyplot", repos = "https://grantmcdermott.r-universe.dev")
    install.packages("tinyplot")
  3. What is tinyplot?

    main

    tinyplot is a lightweight extension of the base R graphics system. It is designed to be a drop-in replacement for the standard plot() function while adding convenience features like:

    • Automatic grouping and legends
    • Faceting (displaying groups in separate plots)
    • Built-in themes
    • Support for various plot types (e.g., density plots, linear models)

    It has zero recursive dependencies and maintains a minimal footprint, making it suitable for package developers who want sophisticated plots without heavy dependencies.

  4. Use tinyplot() and plt() for quick plotting

    main

    The core function tinyplot() (or its shorthand alias plt()) allows you to create plots using formula syntax. It supports grouping via the | operator in formulas and automatic legend generation.

    Grouped scatterplot using formula syntax:

    tinyplot(Sepal.Length ~ Petal.Length | Species, data = iris)

    Grouped scatterplot using the plt() shorthand with aesthetic tweaks:

    plt(
      Sepal.Length ~ Petal.Length | Species, 
      data = iris,
      palette = "dark", pch = 16,
      grid = TRUE, frame = FALSE
    )
    plt(
      Sepal.Length ~ Petal.Length | Species, 
      data = iris,
      palette = "dark", pch = 16,
      grid = TRUE, frame = FALSE
    )
  5. Create faceted scatterplots with continuous legends

    main

    tinyplot supports faceting and continuous color gradients for grouping.

    plt(
      Sepal.Length ~ Petal.Length | Sepal.Length, data = iris,
      facet = ~Species, pch = 19,
      main = "Faceted flowers", sub = "Brought to you by tinyplot"
    )
    plt(
      Sepal.Length ~ Petal.Length | Sepal.Length, data = iris,
      facet = ~Species, pch = 19,
      main = "Faceted flowers", sub = "Brought to you by tinyplot"
    )
  6. Create grouped density plots

    main

    You can create density plots with automatic grouping and filling using the type = "density" and fill = "by" arguments.

    plt(
      ~ Petal.Length | Species,
      data = iris,
      type = "density",
      fill = "by",
      main = "Distribution of petal lengths",
      sub = "Grouped by species"
    )
    plt(
      ~ Petal.Length | Species,
      data = iris,
      type = "density",
      fill = "by",
      main = "Distribution of petal lengths",
      sub = "Grouped by species"
    )
  7. Add layers to plots with plt_add()

    main

    After creating a plot with plt(), you can add layers or statistical transformations using plt_add().

    Example: Adding a linear model (lm) layer:

    plt(Sepal.Length ~ Petal.Length | Species, data = iris)
    plt_add(type = "lm")
    plt(Sepal.Length ~ Petal.Length | Species, data = iris)
    plt_add(type = "lm")
  8. Customize plots with tinytheme()

    main

    tinyplot provides built-in themes for convenient customization. Themes are persistent and will be applied to all subsequent plots until changed or reset.

    Set a theme:

    tinytheme("clean2")

    Reset to default theme:

    tinytheme()
    tinytheme("clean2")