项目作者: hrbrmstr

项目描述 :
:lock_with_ink_pen:意见的,以印刷为中心的ggplot2主题和主题组件
高级语言: R
项目地址: git://github.com/hrbrmstr/hrbrthemes.git
创建时间: 2017-02-11T17:03:01Z
项目社区:https://github.com/hrbrmstr/hrbrthemes

开源协议:Other

下载


Project Status: Active – The project has reached a stable, usable
state and is being actively
developed.
Signed
by
Signed commit
%

cran
checks
CRAN
status
Minimal R
Version
License
downloads
DOI

hrbrthemes

Additional Themes and Theme Components for ‘ggplot2’


This is a very focused package that provides typography-centric themes
and theme components for ggplot2.

The core theme: theme_ipsum (“ipsum” is Latin for “precise”) uses
Arial Narrow which should be installed on practically any modern system,
so it’s “free”-ish. This font is condensed, has solid default kerning
pairs and geometric numbers. That’s what I consider the “font trifecta”
must-have for charts. An additional quality for fonts for charts is that
they have a diversity of weights. Arial Narrow (the one on most systems,
anyway) does not have said diversity but this quality is not (IMO) a
“must have”.

The following functions are implemented/objects are exported:

Themes:

  • theme_ipsum: Arial Narrow
  • theme_ipsum_gs: Goldman Sans Condensed
  • theme_ipsum_es: Econ Sans Condensed
  • theme_ipsum_rc: Roboto Condensed
  • theme_ipsum_ps: IBM Plex Sans font
  • theme_ipsum_pub: Public Sans
  • theme_ipsum_tw: Titilium Web
  • theme_modern_rc: Roboto Condensed dark theme
  • theme_ft_rc: Dark theme based on FT’s dark theme (Roboto Condensed)

Scales (that align with various themes):

  • scale_color_ipsum: Discrete color & fill scales based on the ipsum
    palette
  • scale_colour_ipsum: Discrete color & fill scales based on the ipsum
    palette
  • scale_fill_ipsum: Discrete color & fill scales based on the ipsum
    palette
  • scale_color_ft: Discrete color & fill scales based on the FT palette
  • scale_colour_ft: Discrete color & fill scales based on the FT
    palette
  • scale_fill_ft: Discrete color & fill scales based on the FT palette
  • scale_x_comma: X & Y scales with opinionated presets for percent &
    comma label formats
  • scale_x_percent: X & Y scales with opinionated presets for percent &
    comma label formats
  • scale_y_comma: X & Y scales with opinionated presets for percent &
    comma label formats
  • scale_y_percent: X & Y scales with opinionated presets for percent &
    comma label formats

Palettes/Named Colors:

  • ipsum_pal: A muted, qualitative color palette
  • ft_cols: FT color palette
  • ft_pal: A bright qualitative color palette
  • ft_text_col: FT color palette
  • bit12: A vector of 12 vibrant colors for categorical data
  • bit12_extended: List of 12 color palettes with 9 shades each for
    continuous interpolation

Fonts:

  • font_an: Arial Narrow font name R variable aliases
  • font_es: Econ Sans font name R variable aliases
  • font_es_bold: Econ Sans font name R variable aliases
  • font_es_light: Econ Sans font name R variable aliases
  • font_rc: Roboto Condensed font name R variable aliases
  • font_rc_light: Roboto Condensed font name R variable aliases
  • font_pub: Public Sans font name R variable aliases
  • font_pub_bold: Public Sans font name R variable aliases
  • font_pub_light: Public Sans font name R variable aliases
  • font_pub_thin: Public Sans font name R variable aliases
  • font_ps: PlexSans font name R variable aliases
  • font_ps_light: PlexSans font name R variable aliases

Flexoki Color Scales:

  • scale_fill_flexoki_continuous: Continuous Fill Scale Using Flexoki
    Colors
  • scale_fill_flexoki_dark: Discrete Fill Scale Using Flexoki Dark
    Colors
  • scale_fill_flexoki_dark_distiller: Distiller Fill Scale Using
    Flexoki Dark Colors
  • scale_fill_flexoki_dark_spectrum: Distiller Fill Scale Across All
    Flexoki Dark Colors
  • scale_fill_flexoki_light: Discrete Fill Scale Using Flexoki Light
    Colors
  • scale_fill_flexoki_light_distiller: Distiller Fill Scale Using
    Flexoki Light Colors
  • scale_fill_flexoki_light_spectrum: Distiller Fill Scale Across All
    Flexoki Light Colors

Bit12 Color Scales:

  • scale_color_bit12: Discrete Color Scale Using Bit12 Colors
  • scale_fill_bit12: Discrete Fill Scale Using Bit12 Colors
  • scale_color_bit12_continuous: Continuous Color Scale Using Bit12
    Colors
  • scale_fill_bit12_continuous: Continuous Fill Scale Using Bit12
    Colors
  • scale_color_bit12_distiller: Distiller Color Scale Using Bit12
    Colors
  • scale_fill_bit12_distiller: Distiller Fill Scale Using Bit12 Colors
  • scale_color_bit12_spectrum: Distiller Color Scale Across All Bit12
    Colors
  • scale_fill_bit12_spectrum: Distiller Fill Scale Across All Bit12
    Colors

Flexoki Palettes:

  • flexoki_light: Vector of 8 colors in the light variant
  • flexoki_dark: Vector of 8 colors in the dark variant
  • flexoki_extended: List of 8 color palettes with 13 shades each for
    continuous interpolation

Utilities:

  • flush_ticks: Makes axis text labels flush on the ends
  • ft_geom_defaults: Change geom defaults from black to custom lights
    for the FT theme
  • gg_check: Spell check ggplot2 plot labels
  • import_econ_sans: Import Econ Sans Condensed font for use in charts
  • import_plex_sans: Import IBM Plex Sans font for use in charts
  • import_roboto_condensed: Import Roboto Condensed font for use in
    charts
  • modern_geom_defaults: Change geom defaults from black to white for
    the modern theme
  • update_geom_font_defaults: Update matching font defaults for text
    geoms

Installation

  1. install.packages("hrbrthemes") # NOTE: CRAN version is 0.8.7
  2. # or
  3. remotes::install_git("https://codeberg.org/hrbrmstr/hrbrthemes.git")

NOTE: To use the ‘remotes’ install options you will need to have the
{remotes} package installed.

Usage

  1. library(hrbrthemes)
  2. library(gcookbook)
  3. library(tidyverse)
  4. # current version
  5. packageVersion("hrbrthemes")
  6. ## [1] '0.9.2'

Base theme (Arial Narrow)

  1. ggplot(mtcars, aes(mpg, wt)) +
  2. geom_point() +
  3. labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
  4. title="Seminal ggplot2 scatterplot example",
  5. subtitle="A plot that is only useful for demonstration purposes",
  6. caption="Brought to you by the letter 'g'") +
  7. theme_ipsum()

Roboto Condensed

  1. ggplot(mtcars, aes(mpg, wt)) +
  2. geom_point() +
  3. labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
  4. title="Seminal ggplot2 scatterplot example",
  5. subtitle="A plot that is only useful for demonstration purposes",
  6. caption="Brought to you by the letter 'g'") +
  7. theme_ipsum_rc()

New FT Theme!

  1. ggplot(mtcars, aes(mpg, wt)) +
  2. geom_point(color = ft_cols$yellow) +
  3. labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
  4. title="Seminal ggplot2 scatterplot example",
  5. subtitle="A plot that is only useful for demonstration purposes",
  6. caption="Brought to you by the letter 'g'") +
  7. theme_ft_rc()

IBM Plex Sans

  1. ggplot(mpg, aes(displ, hwy)) +
  2. geom_jitter(aes(color=class, fill=class), size=3, shape=21, alpha=1/2) +
  3. scale_x_continuous(expand=c(0,0), limits=c(1, 8), breaks=1:8) +
  4. scale_y_continuous(expand=c(0,0), limits=c(10, 50)) +
  5. scale_color_ipsum() +
  6. scale_fill_ipsum() +
  7. facet_wrap(~class, scales="free") +
  8. labs(
  9. title="IBM Plex Sans Test",
  10. subtitle="This is a subtitle to see the how it looks in IBM Plex Sans",
  11. caption="Source: hrbrthemes & IBM"
  12. ) +
  13. theme_ipsum_ps(grid="XY", axis="xy") +
  14. theme(legend.position="none") -> gg
  15. flush_ticks(gg)
  16. ## theme(axis.text.x=element_text(hjust=c(0, rep(0.5, 6), 1))) +
  17. ## theme(axis.text.y=element_text(vjust=c(0, rep(0.5, 3), 1)))

Scales (Color/Fill)

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

Scales (Axis)

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

  1. ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup)) +
  2. geom_area() +
  3. scale_fill_ipsum() +
  4. scale_x_continuous(expand=c(0,0)) +
  5. scale_y_comma() +
  6. labs(title="Age distribution of population in the U.S., 1900-2002",
  7. subtitle="Example data from the R Graphics Cookbook",
  8. caption="Source: R Graphics Cookbook") +
  9. theme_ipsum_rc(grid="XY") +
  10. theme(axis.text.x=element_text(hjust=c(0, 0.5, 0.5, 0.5, 1))) +
  11. theme(legend.position="bottom")

  1. update_geom_font_defaults(font_rc_light)
  2. count(mpg, class) %>%
  3. mutate(n=n*2000) %>%
  4. arrange(n) %>%
  5. mutate(class=factor(class, levels=class)) %>%
  6. ggplot(aes(class, n)) +
  7. geom_col() +
  8. geom_text(aes(label=scales::comma(n)), hjust=0, nudge_y=2000) +
  9. scale_y_comma(limits=c(0,150000)) +
  10. coord_flip() +
  11. labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
  12. title="Seminal ggplot2 column chart example with commas",
  13. subtitle="A plot that is only useful for demonstration purposes, esp since you'd never\nreally want direct labels and axis labels",
  14. caption="Brought to you by the letter 'g'") +
  15. theme_ipsum_rc(grid="X")

Spellcheck ggplot2 labels

  1. df <- data.frame(x=c(20, 25, 30), y=c(4, 4, 4), txt=c("One", "Two", "Three"))
  2. ggplot(mtcars, aes(mpg, wt)) +
  3. geom_point() +
  4. labs(x="This is some txt", y="This is more text",
  5. title="Thisy is a titlle",
  6. subtitle="This is a subtitley",
  7. caption="This is a captien") +
  8. theme_ipsum_rc(grid="XY") -> gg
  9. gg_check(gg)
  10. ## Possible misspelled words in [title]: (Thisy, titlle)
  11. ## Possible misspelled words in [subtitle]: (subtitley)
  12. ## Possible misspelled words in [caption]: (captien)

Flexoki Color Scales

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

  1. # Discrete colors example with light variant
  2. ggplot(mpg, aes(class, fill=class)) +
  3. geom_bar() +
  4. labs(title="Flexoki Light Colors",
  5. subtitle="Discrete color scale example",
  6. caption="Using scale_fill_flexoki_light()") +
  7. theme_ipsum() +
  8. scale_fill_flexoki_light() +
  9. theme(legend.position="none")

  1. # Dark variant with points
  2. ggplot(mpg, aes(displ, hwy, color=class)) +
  3. geom_point(size=3) +
  4. labs(title="Flexoki Dark Colors",
  5. subtitle="Another discrete color example",
  6. caption="Using scale_color_flexoki_dark()") +
  7. theme_ipsum_rc() +
  8. scale_color_flexoki_dark()

  1. library(patchwork)
  2. data.frame(
  3. x = rep(1:50, 50),
  4. y = rep(1:50, each=50),
  5. z = sqrt((rep(1:50, 50)-25)^2 + (rep(1:50, each=50)-25)^2)
  6. ) -> grad_df
  7. names(flexoki_dark) |>
  8. map(\(.p) {
  9. ggplot(grad_df, aes(x, y, fill=z)) +
  10. geom_tile() +
  11. scale_fill_flexoki_continuous(palette = .p) +
  12. labs(
  13. x = NULL, y = NULL
  14. ) +
  15. theme_ipsum(grid="") +
  16. theme(legend.position = "none")
  17. }) |>
  18. do.call(what = "wrap_plots") +
  19. plot_layout(ncol = 4) +
  20. plot_annotation(
  21. title="Flexoki Continuous Color Scales",
  22. subtitle="All 8 continuous color palettes",
  23. caption="Using scale_fill_flexoki_continuous()",
  24. theme = theme_ipsum(grid="")
  25. )

Bit12 Color Scales

The Bit12 color system provides a set of 12 distinct colors optimized
for data visualization.

  1. # Discrete colors example
  2. ggplot(mpg, aes(class, fill=class)) +
  3. geom_bar() +
  4. labs(title="Bit12 Color Palette",
  5. subtitle="A vibrant 12-color categorical palette",
  6. caption="Using scale_fill_bit12()") +
  7. theme_ipsum() +
  8. scale_fill_bit12() +
  9. theme(legend.position="none")

  1. library(patchwork)
  2. # Taking a subset of the palette for this example
  3. names(bit12)[c(1,3,5,7,9,11)] |>
  4. map(\(.p) {
  5. ggplot(grad_df, aes(x, y, fill=z)) +
  6. geom_tile() +
  7. scale_fill_bit12_continuous(palette = .p) +
  8. labs(
  9. x = NULL, y = NULL, title = .p
  10. ) +
  11. theme_ipsum(grid="") +
  12. theme(legend.position = "none")
  13. }) |>
  14. do.call(what = "wrap_plots") +
  15. plot_layout(ncol = 3) +
  16. plot_annotation(
  17. title="Bit12 Continuous Color Scales",
  18. subtitle="Selected continuous color palettes",
  19. caption="Using scale_fill_bit12_continuous()",
  20. theme = theme_ipsum(grid="")
  21. )

hrbrthemes Metrics

Lang # Files (%) LoC (%) Blank lines (%) # Lines (%)
R 23 0.44 1840 0.46 339 0.5 1290 0.5
SVG 2 0.04 150 0.04 0 0.0 0 0.0
JSON 1 0.02 15 0.00 0 0.0 0 0.0
SUM 26 0.50 2005 0.50 339 0.5 1290 0.5

{cloc} 📦 metrics for hrbrthemes

Code of Conduct

Please note that this project is released with a Contributor Code of
Conduct. By participating in this project you agree to abide by its
terms.