Skip to content

Instantly share code, notes, and snippets.

View teunbrand's full-sized avatar

Teun van den Brand teunbrand

View GitHub Profile
@teunbrand
teunbrand / guide_marquee.R
Last active June 2, 2024 06:36
Proof of concept for a guide based on {marquee}. It can interpolate text with symbols created as a legend and recolour parts of text to match a scale's colour.
library(ggplot2)
library(marquee)
library(rlang)
library(grid)
library(gtable)
# Constructor -------------------------------------------------------------
#' Text guide
#'
@teunbrand
teunbrand / rle_2d.R
Last active October 20, 2023 13:34
First attempt at 2D run-length encoding
rle_2d <- function(matrix) {
# If we have an empty matrix, we return 0-row data.frame
if (prod(dim(matrix)) < 1) {
ans <- data.frame(
col.start = integer(),
col.end = integer(),
row.start = integer(),
row.end = integer(),
value = as.vector(matrix)
@teunbrand
teunbrand / tryout_epoxy
Created October 6, 2023 10:32
Playing with {ggtext} and {epoxy}
library(epoxy)
library(ggplot2)
library(ggtext)
# Creating an epoxy theme element
element_epoxy <- function(...) {
el <- ggtext::element_markdown(...)
class(el) <- union("element_epoxy", class(el))
el
}
@teunbrand
teunbrand / text_curved.R
Created November 13, 2021 14:36
Example code of text on path
library(grid)
library(scales)
library(textshaping)
label <- "Here be some text on a path"
# Setup some curve
t <- seq(10, 0, by = -0.05)
curve <- data.frame(
@teunbrand
teunbrand / ggplot_svg_keys.R
Last active November 4, 2021 10:36
Drawing the keys of a ggplot2 legend from SVG files
# Load libraries
library(ggplot2)
library(grid)
library(rlang)
library(grImport2)
# Replace by intended SVG file
file <- "https://upload.wikimedia.org/wikipedia/commons/d/db/Brain_Drawing.svg"
download.file(file, tmp <- tempfile(fileext = ".svg"))
@teunbrand
teunbrand / accidental_aRt.R
Created September 2, 2021 09:23
Code for making fourier transform mistakes and producing plots for weird wavy patterns.
library(ggplot2)
accidental_art <- function(x, r = 1.9) {
x <- cor(x)
n <- dim(x)[1]
mask <- matrix(0, ncol(x), nrow(x))
rad <- n/2
xcen <- 0
ycen <- 0
library(grid)
library(scales)
library(gtable)
library(rlang)
# Constructor -------------------------------------------------------------
#' Combination matrix axis
#'
#' @inheritParams guide_axis
@teunbrand
teunbrand / rasterising_ggplot_layers.R
Last active July 10, 2020 09:13
Functions and examples on rasterising ggplot2 layers
# Preface:
# This document has two function to rasterise ggplot2 layers.
# The functions are very similar in structure to those in thomas85's ggfx package (https://github.com/thomasp85/ggfx),
# The ggfx package is licenced under an MIT licence, so I think this should be OK.
library(ggplot2)
library(grid)
library(ragg)
library(png)
@teunbrand
teunbrand / midiplot.R
Last active July 5, 2020 00:37
Making a plot of a midi file
library(ggplot2)
library(tuneR)
midi <- tuneR::readMidi("https://www.8notes.com/school/midi/piano/beethoven_ode_to_joy.mid")
df <- tuneR::getMidiNotes(midi)
# Difference between first two notes
mult <- 960