ggplot2 charts, hand-drawn

Two ways in

sketch() draws any plot in Excalidraw style, at the size of whatever device it lands on, so it works in the plot pane, in knitr chunks and with ggsave().

p <- ggplot(mpg, aes(class, fill = drv)) + geom_bar() +
  scale_fill_excalidraw() + labs(title = "Drive train by class", x = NULL)
sketch(p)

A hand-drawn stacked bar chart titled Drive train by class, bars filled in pastel Excalidraw colours

ggsave("drive.png", sketch(p), width = 8, height = 5)

as_excalidraw() returns the scene itself, ready to annotate, restyle, save or open in Excalidraw:

s <- as_excalidraw(p)
s

The bar chart as an Excalidraw scene, drawn in hand-drawn style

By default plots get theme_excalidraw() (handwriting font, ink axes, no grey panel). Pass theme = NULL to keep your own theme. Other options: fill_style, roughness, font, outline, preset and seed.

as_excalidraw(p, fill_style = "cross-hatch", roughness = "cartoonist", font = "Virgil") |>
  plot()

The bar chart again with cross-hatched fills, extra-sloppy cartoonist lines and the Virgil font

Annotations with data values

Scenes made from a ggplot remember how data values map to positions, so annotations can point at the data. Categories map to their centre; wrap a number in I() to give pixels instead.

cars <- ggplot(mpg, aes(class)) + geom_bar(fill = "#a5d8ff") + labs(x = NULL, y = "cars")
as_excalidraw(cars) |>
  exc_title("What do people drive?") |>
  exc_mark(x = "suv", y = c(0, 62), type = "box") |>
  exc_mark(x = "compact", y = 47, type = "circle") |>
  exc_note("SUVs win!", x = "midsize", y = 52, to = c("suv", 50)) |>
  exc_callout("only 5 of these", x = "2seater", y = 5, dx = 30, dy = -70) |>
  plot()

A hand-drawn bar chart of cars by class titled What do people drive?, with a box around the SUV bar, a circle on compact, a sticky note saying SUVs win! with an arrow, and a callout saying only 5 of these by the 2seater bar

exc_mark() types: "circle", "box", "underline", "highlight", "strike", "bracket", "cross".

Colours

excalidraw_colors("stroke", 6)
#> [1] "#1971c2" "#e03131" "#2f9e44" "#f08c00" "#9c36b5" "#0c8599"
excalidraw_colors("background", 6)
#> [1] "#a5d8ff" "#ffc9c9" "#b2f2bb" "#ffec99" "#eebefa" "#99e9f2"

scale_fill_excalidraw() uses the light fills and scale_colour_excalidraw() the strong strokes; the _c versions run through the shades of one hue.

Base graphics

sketch(~ hist(faithful$waiting, col = "#ffd8a8", main = "Waiting time", xlab = "minutes"))

A hand-drawn histogram of Old Faithful waiting times in minutes, bars filled in light orange

Presets

sketch(ggplot(mpg, aes(displ, hwy, colour = drv)) + geom_point(size = 2.5) +
         scale_colour_excalidraw(), preset = "blueprint")

A hand-drawn scatter plot of highway mileage against engine size, coloured by drive train, in the blueprint preset: white lines on blue