Skip to content

Instantly share code, notes, and snippets.

@perlatex
Created December 24, 2021 14:04
Show Gist options
  • Save perlatex/95a1358da4d3d1ff444af098e17a2efd to your computer and use it in GitHub Desktop.
Save perlatex/95a1358da4d3d1ff444af098e17a2efd to your computer and use it in GitHub Desktop.
library(tidyverse)
treesize <- 10
trunkheight <- 5
trunkwidth <- 2.5
tree <-
tibble(
x = rep(treesize:1, each = 2),
y = seq(1, treesize * 2, by = 1),
color = "darkgreen"
)
trunk <- tibble(
x = rep(trunkwidth, each = trunkheight),
y = seq(0, -(trunkheight - 1), by = -1),
color = "chocolate4"
)
lights <- tree %>%
select(x, y) %>%
mutate(x = map(x, ~ seq(-.x, .x, by = 1))) %>%
unnest(x) %>%
slice_sample(prop = 0.25)
lights <- lights %>%
mutate(color = sample(x = c(1:3), size = nrow(lights), replace = TRUE)) %>%
mutate(size = sample(x = c(1:3), size = nrow(lights), replace = TRUE)) %>%
mutate(across(c(color), as.factor))
lights
bind_rows(tree, trunk) %>%
ggplot() +
geom_rect(
aes(xmin = -x, ymin = y - 0.5, xmax = x, ymax = y + 0.5, fill = color)
) +
geom_point(data = lights, aes(x = x, y = y, color = color, size = size)) +
scale_fill_identity() +
scale_color_manual(values = c("firebrick2", "gold", "dodgerblue3")) +
scale_size_area(max_size = 5) +
theme_void() +
theme(legend.position = "none") +
annotate("text", x = 1, y = 23, label = "祝大家圣诞快乐", size = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment