hrbrthemes

repository·master·Indexed 23 days ago

https://github.com/hrbrmstr/hrbrthemes

An R package providing professional, typography-centric themes and color scales for ggplot2 visualizations. It includes a variety of themes based on fonts like Arial Narrow, Roboto Condensed, and IBM Plex Sans, as well as specialized color systems such as Flexoki and Bit12. The package also provides utilities like gg_check() for spell-checking plot labels and flush_ticks() for aligning axis text.

Tokens
2.5K
Snippets
9
Records
16
Agent score
30%

What's inside hrbrthemes

  1. Use hrbrthemes in ggplot2

    master

    To use the themes and scales, load the library along with tidyverse or ggplot2.

    library(hrbrthemes)
    library(tidyverse)
    
    # Example: Using the base theme (Arial Narrow)
    ggplot(mtcars, aes(mpg, wt)) +
      geom_point() +
      labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
           title="Seminal ggplot2 scatterplot example",
           subtitle="A plot that is only useful for demonstration purposes",
           caption="Brought to you by the letter 'g'") +
      theme_ipsum()
    library(hrbrthemes)
    library(gcookbook)
    library(tidyverse)
  2. Install hrbrthemes

    master

    You can install hrbrthemes from CRAN or directly from Codeberg using remotes.

    From CRAN:

    install.packages("hrbrthemes")

    From Codeberg (requires remotes package):

    remotes::install_git("https://codeberg.org/hrbrmstr/hrbrthemes.git")
  3. Overview of hrbrthemes

    master
    hrbrthemes is an R package that provides typography-centric themes and theme components specifically designed for ggplot2. It focuses on high-quality typography, offering various themes based on different fonts (like Arial Narrow, Roboto Condensed, and IBM Plex Sans) and specialized color scales (like Flexoki and Bit12) to improve the aesthetic quality of data visualizations.
  4. Align axis text with flush_ticks()

    master

    The flush_ticks() utility makes axis text labels flush on the ends of the axis, which is useful for cleaning up plot layouts.

    # Example usage on a ggplot object
    flush_ticks(gg)
    # Equivalent to:
    # theme(axis.text.x=element_text(hjust=c(0, rep(0.5, 6), 1))) +
    # theme(axis.text.y=element_text(vjust=c(0, rep(0.5, 3), 1)))
  5. Spellcheck ggplot2 labels with gg_check()

    master

    The gg_check() utility allows you to spell check the text elements (title, subtitle, caption, etc.) of a ggplot2 object.

    df <- data.frame(x=c(20, 25, 30), y=c(4, 4, 4), txt=c("One", "Two", "Three"))
    
    gg <- ggplot(mtcars, aes(mpg, wt)) +
      geom_point() +
      labs(x="This is some txt", y="This is more text",
           title="Thisy is a titlle",
           subtitle="This is a subtitley",
           caption="This is a captien") +
      theme_ipsum_rc(grid="XY")
    
    gg_check(gg)
    df <- data.frame(x=c(20, 25, 30), y=c(4, 4, 4), txt=c("One", "Two", "Three"))
    
    ggplot(mtcars, aes(mpg, wt)) +
      geom_point() +
      labs(x="This is some txt", y="This is more text",
           title="Thisy is a titlle",
           subtitle="This is a subtitley",
           caption="This is a captien") +
      theme_ipsum_rc(grid="XY") -> gg
    
    gg_check(gg)
  6. Flexoki Color Scales

    master

    The Flexoki color system provides discrete and continuous scales in light and dark variants.

    Light Variant:

    • scale_fill_flexoki_light: Discrete Fill Scale.
    • scale_fill_flexoki_light_distiller: Distiller Fill Scale.
    • scale_fill_flexoki_light_spectrum: Distiller Fill Scale across all colors.

    Dark Variant:

    • scale_fill_flexoki_dark: Discrete Fill Scale.
    • scale_fill_flexoki_dark_distiller: Distiller Fill Scale.
    • scale_fill_flexoki_dark_spectrum: Distiller Fill Scale across all colors.

    Continuous:

    • scale_fill_flexoki_continuous: Continuous Fill Scale using Flexoki colors.
  7. Bit12 Color Scales

    master

    The Bit12 system provides 12 vibrant colors optimized for categorical data visualization.

    • scale_color_bit12 / scale_fill_bit12: Discrete scales.
    • scale_color_bit12_continuous / scale_fill_bit12_continuous: Continuous scales.
    • scale_color_bit12_distiller / scale_fill_bit12_distiller: Distiller scales.
    • scale_color_bit12_spectrum / scale_fill_bit12_spectrum: Spectrum scales.
  8. Format axis labels with scale_y_percent()

    master

    Use scale_y_percent() to automatically format Y-axis labels as percentages with appropriate comma separators.

    count(mpg, class) %>%
      mutate(pct=n/sum(n)) %>%
      ggplot(aes(class, pct)) +
      geom_col() +
      scale_y_percent() +
      labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
           title="Seminal ggplot2 column chart example with percents",
           subtitle="A plot that is only useful for demonstration purposes",
           caption="Brought to you by the letter 'g'") +
      theme_ipsum(grid="Y")
  9. Spell check ggplot2 labels with gg_check()

    master

    The gg_check() utility allows you to perform a spell check on the text elements (title, subtitle, caption, axis labels) of a ggplot2 object.

    df <- data.frame(x=c(20, 25, 30), y=c(4, 4, 4), txt=c("One", "Two", "Three"))
    
    ggplot(mtcars, aes(mpg, wt)) +
      geom_point() +
      labs(x="This is some txt", y="This is more text",
           title="Thisy is a titlle",
           subtitle="This is a subtitley",
           caption="This is a captien") +
      theme_ipsum_rc(grid="XY") -> gg
    
    gg_check(gg)
  10. Update geom font defaults with update_geom_font_defaults()

    master

    Use update_geom_font_defaults() to globally change the font used by text geoms (like geom_text) to a specific font alias provided by the package.

    update_geom_font_defaults(font_rc_light)
  11. Use scale_color_ipsum() for discrete colors

    master

    To apply the ipsum color palette to discrete data, use scale_color_ipsum() within your ggplot call.

    ggplot(mtcars, aes(mpg, wt)) +
      geom_point(aes(color=factor(carb))) +
      labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
           title="Seminal ggplot2 scatterplot example",
           subtitle="A plot that is only useful for demonstration purposes",
           caption="Brought to you by the letter 'g'") +
      scale_color_ipsum() +
      theme_ipsum_rc()