Skip to content

Instantly share code, notes, and snippets.

@jonspring
Created March 20, 2021 06:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonspring/9b69a219d0406828530723f286d21bc8 to your computer and use it in GitHub Desktop.
Save jonspring/9b69a219d0406828530723f286d21bc8 to your computer and use it in GitHub Desktop.
# Inspired by Nadieh Bremer's lasso annotations here: https://twitter.com/NadiehBremer/status/1277622602735865856
# h/t Cedric Scherer: https://twitter.com/CedScherer/status/1278351840074240001
library(ggplot2); library(ggforce)
add_lasso_layer <- function(x1, x2, y) {
width = x2 - x1
space = width * 0.05
lasso_points = data.frame(
x = c(x1, x1-space, x1-space, x1+width/2, x2-width/2, x2+space, x2+space, x2),
y = c(y, y - space/10,rep(y-space, 4), y - space/10, y),
id = rep(1:2, each = 4))
list(ggforce::geom_bezier(data = lasso_points, aes(x = x, y = y, group = id)),
geom_segment(aes(x = x2 + space*0.45, xend = x2 + width/2,
y = y - space/6, yend = y - space/6)))
}
ggplot() +
geom_tile(aes(x = 0, y = 0, width = 2, height = 2), fill = "wheat") +
add_lasso_layer(-1, 1, 0) +
coord_equal() +
theme_void()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment