Created
May 30, 2020 21:26
-
-
Save djnavarro/daf4b759c54c57c9fe48c3a811da1974 to your computer and use it in GitHub Desktop.
minimal voronoi tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(flametree) # github.com/djnavarro/flametree | |
library(voronoise) # github.com/djnavarro/voronoise | |
library(dplyr) | |
# set seed | |
seed <- 1 | |
set.seed(seed) | |
# the "flametree" itself | |
ftree <- flametree_grow( | |
seed = seed, | |
angle = c(-2:4) * 10, | |
scale = c(.6, .8, .9) | |
) | |
# "leaf" coordinates are at terminal locations (id_step = 2) | |
# on the terminal branches (id_leaf == TRUE) in the tree | |
vleaf <- ftree %>% filter(id_leaf == TRUE, id_step == 2) | |
# a simple "perturb" function: drop each leaf to the ground | |
leaf_fall <- function(data) { | |
data %>% | |
group_by(group) %>% | |
mutate(y = y - min(y)) %>% | |
ungroup() | |
} | |
# create the plot... | |
p <- ggplot() + | |
# tree trunk is drawn using geom_bezier from the | |
# ggforce package (loaded by voronoise) | |
geom_bezier( | |
data = ftree, | |
mapping = aes( | |
x = coord_x, | |
y = coord_y, | |
group = id_path, | |
size = seg_wid | |
), | |
lineend = "round", | |
show.legend = FALSE | |
) + | |
# add points drawn at the leaves | |
geom_point( | |
data = vleaf, | |
mapping = aes( | |
x = coord_x, | |
y = coord_y | |
), | |
size = 8 | |
) + | |
# add voronoi tiles with no perturbation | |
geom_voronoise( | |
data = vleaf, | |
mapping = aes( | |
x = coord_x, | |
y = coord_y | |
), | |
max.radius = .2, | |
fill = "#ffffffcc", | |
colour = "black", | |
size = 2 | |
) + | |
# add voronoi tiles for falling leaves | |
geom_voronoise( | |
data = vleaf, | |
mapping = aes( | |
x = coord_x, | |
y = coord_y | |
), | |
max.radius = .2, | |
fill = "#ffffffcc", | |
colour = "black", | |
size = 2, | |
perturb = leaf_fall | |
) + | |
# styling | |
theme_void() + | |
coord_equal() | |
# save the file | |
ggsave( | |
filename = "~/Desktop/vtree_03.png", | |
plot = p, | |
width = 100/3, | |
height = 100/3, | |
dpi = 150 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried both. By putting "dependencies = FALSE" and with out it. But it didn't work.
By the way my R Studio version is 3.6.1.