Package 'ggrough2'

Title: Hand-Drawn Rendering for 'ggplot2' via SVG and 'Rough.js'
Description: Converts 'ggplot2' plots to sketch-style graphics by rendering to SVG with 'svglite' and post-processing the result with 'svg2rough.js'.
Authors: David Schoch [aut, cre] (ORCID: <https://orcid.org/0000-0003-2952-4812>)
Maintainer: David Schoch <[email protected]>
License: MIT + file LICENSE
Version: 0.0.0.9000
Built: 2026-05-24 06:15:52 UTC
Source: https://github.com/schochastics/ggrough2

Help Index


Add a Google Font for use with rough_plot()

Description

Downloads a font from Google Fonts and caches it locally so that rough_plot(p, font = family) can embed it. Requires an internet connection on first use; subsequent calls for the same family are instant.

Usage

add_google_font(name, family = name)

Arguments

name

Google Fonts name, e.g. "Dancing Script".

family

Font family name to use later in rough_plot(). Defaults to name.

Value

The family name, invisibly.


Rough.js drawing options

Description

Constructs a list of optional Rough.js parameters to pass to rough_plot(). All arguments default to NULL, meaning the Rough.js library default is used.

Usage

rough_options(
  fill_weight = NULL,
  hachure_angle = NULL,
  hachure_gap = NULL,
  curve_step_count = NULL,
  curve_fitting = NULL,
  stroke_line_dash = NULL,
  stroke_line_dash_offset = NULL,
  fill_line_dash = NULL,
  fill_line_dash_offset = NULL,
  disable_multi_stroke = NULL,
  disable_multi_stroke_fill = NULL,
  simplification = NULL,
  dash_offset = NULL,
  dash_gap = NULL,
  zigzag_offset = NULL,
  preserve_vertices = NULL
)

Arguments

fill_weight

Width of hachure lines (numeric). For "dots" fill style, this is the dot diameter. Defaults to half the stroke width.

hachure_angle

Angle of hachure lines in degrees (numeric). Default: -41.

hachure_gap

Average gap in pixels between hachure lines (numeric). Default: 4× stroke width.

curve_step_count

Number of points used to approximate curves (ellipses, arcs). Default: 9.

curve_fitting

How closely rendered dimensions match specified dimensions for curves (0–1). Default: 0.95.

stroke_line_dash

Dashed stroke pattern — a numeric vector of dash/gap lengths (e.g. c(5, 10)). Does not affect fill hachure.

stroke_line_dash_offset

Phase offset for dashed strokes (numeric).

fill_line_dash

Like stroke_line_dash but affects fill patterns.

fill_line_dash_offset

Like stroke_line_dash_offset but for fills.

disable_multi_stroke

Logical. If TRUE, only a single stroke pass is used (less sketchy). Default: FALSE.

disable_multi_stroke_fill

Logical. If TRUE, single stroke for hachure fill lines. Default: FALSE.

simplification

Simplify SVG paths by this factor (0–1). 0 = no simplification. Useful for giving complex paths a sketchy feel.

dash_offset

Nominal dash length (px) for "dashed" fill style. Defaults to hachure_gap.

dash_gap

Nominal gap between dashes (px) for "dashed" fill style. Defaults to hachure_gap.

zigzag_offset

Nominal width of zigzag triangles (px) for "zigzag-line" fill style. Defaults to hachure_gap.

preserve_vertices

Logical. If TRUE, endpoint positions of lines and curves are not randomised. Default: FALSE.

Value

A named list suitable for the options argument of rough_plot().


Render a ggplot as a hand-drawn sketch

Description

Render a ggplot as a hand-drawn sketch

Usage

rough_plot(
  plot,
  width = 7,
  height = 5,
  roughness = 1.5,
  bowing = 1,
  fill_style = "hachure",
  bg_fill_style = "solid",
  seed = NULL,
  preserve_text = TRUE,
  font = "IndieFlower",
  options = rough_options()
)

Arguments

plot

A ggplot object.

width, height

Plot size in inches.

roughness

Rough.js roughness parameter.

bowing

Rough.js bowing parameter.

fill_style

Rough.js fill style for data elements (geoms).

bg_fill_style

Rough.js fill style for background elements (panel/plot backgrounds). Defaults to "solid". Use the same value as fill_style to apply a uniform style to everything.

seed

Optional seed for deterministic randomness.

preserve_text

Keep text unchanged.

font

Font family name to use for text labels, or NULL to leave the plot's original fonts unchanged. Defaults to "IndieFlower", the bundled hand-drawn font. Any system font name (e.g. "Arial") works directly. For Google Fonts, first call add_google_font().

options

A list of additional Rough.js drawing options, typically created with rough_options(). Controls parameters such as fill_weight, hachure_angle, hachure_gap, simplification, etc.

Value

An htmlwidget.


Save a roughened plot as HTML

Description

Save a roughened plot as HTML

Usage

save_rough_html(plot, file, ...)

Arguments

plot

A ggplot object.

file

Output HTML file.

...

Passed to rough_plot().


Export a roughened plot as an SVG or PNG file

Description

Renders the plot in a headless browser so the Rough.js sketch is fully drawn before the image is captured. Requires the webshot2 package and a local Chrome/Chromium installation.

Usage

save_rough_image(plot, file, ..., delay = 2, vwidth = 992, vheight = 744)

Arguments

plot

A ggplot object.

file

Output file path. Extension must be .svg or .png.

...

Additional arguments passed to rough_plot().

delay

Seconds to wait after page load before capturing. Increase if the output is blank or partially rendered.

vwidth, vheight

Browser viewport dimensions in pixels.

Value

file, invisibly.