Skip to content

Instantly share code, notes, and snippets.

@jthomasmock
Created November 11, 2021 18:47
Show Gist options
  • Save jthomasmock/e574d2c4a262dde862c90fc28aef6c7d to your computer and use it in GitHub Desktop.
Save jthomasmock/e574d2c4a262dde862c90fc28aef6c7d to your computer and use it in GitHub Desktop.
gen_plot <- function(xvals = c(1,12), yvals = c(1,10), n = 10,
distance = seq(-3,3,length.out =n),
seed = NULL, palette = c("red", "blue")){
# adapted from: https://stackoverflow.com/questions/50275195/
segment.shift <- function(x1,x2,y1,y2, d){
# calculate vector
v <- c(x2 - x1,y2 - y1)
# normalize vector
v <- v/sqrt((v[1]**2 + v[2]**2))
# perpendicular unit vector
vnp <- c( -v[2], v[1] )
list(
x1 = x1 + d*vnp[1],
x2 = x2 + d*vnp[1],
y1 = y1 + d*vnp[2],
y2 = y2 + d*vnp[2]
)
}
set.seed(seed)
my_df <- tibble(
x1 = rep(xvals[1],n),
x2 = rep(xvals[2],n),
y1 = rep(yvals[1],n),
y2 = rep(yvals[2],n),
d = distance
) %>%
pmap_dfr(segment.shift) %>%
mutate(color = scales::col_numeric(palette = palette, domain = NULL)(x1))
rand_vals <- my_df %>%
select(x1:y2) %>%
pmap_dfr(function(x1,x2,y1,y2){
# find points on the line
approx_vals <- approx(x=c(x1,x2), y =c(y1,y2), n = 50)
# randomly shorten the line
sub_vec <- sample(8:35, size = 1)
# randomly select a "point" ahead of the shortened portion
point_vec <- sub_vec - sample(3:8, size = 1, prob = seq(0.5, 0.1,length.out = 6))
# grab the x/ycoords for the lines
xval <- approx_vals$x[sub_vec]
yval <- approx_vals$y[sub_vec]
# grab the x/ycoords for the points
xpt <- approx_vals$x[point_vec]
ypt <- approx_vals$y[point_vec]
# throw it in a list
list(xstart = xval, ystart = yval, xpt =xpt, ypt = ypt)
})
my_df %>%
# extract the line/points coords
mutate(x1 = rand_vals$xstart, y1 = rand_vals$ystart,
xpt = rand_vals$xpt, ypt = rand_vals$ypt) %>%
ggplot() +
geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, color = I(color)),
size = 8, lineend = "round") +
geom_point(aes(x = xpt, y = ypt, color = I(color)), size = 8) +
theme_void() +
theme(plot.background = element_rect(fill = "white",color="white"),
panel.background = element_rect(fill = "white",color="white"))
}
my_pal <- c("#f55a07", "#fa1500", "#942b2d", "#ee5299", "#c41a6c", "#742684",
"#00437c", "#0179e3", "#005f2e", "#809d29", "#3f8292", "#565656",
"#f9de24", "#d08b3e", "#f95a00")
my_plot <- gen_plot(n = 15, palette = my_pal, distance = seq(-4,4.5, length.out =15),
seed = 1234) +
coord_cartesian(xlim = c(-2,11), ylim = c(-2,8))
ggsave("myplot.png", my_plot, dpi = "retina", height = 6, width = 6)
@jthomasmock
Copy link
Author

A ggplot of 15 thick parallel lines with a large point in front of each line. The lines are flowing from the top right towards the bottom left/middle, and each line is colored with it's own portion of a rainbow palette including ROYGBIV along with browns and greys. Each line is of random length with a slightly extended point ahead of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment