Skip to content

Instantly share code, notes, and snippets.

@djnavarro
Created May 30, 2020 21:26
Show Gist options
  • Save djnavarro/daf4b759c54c57c9fe48c3a811da1974 to your computer and use it in GitHub Desktop.
Save djnavarro/daf4b759c54c57c9fe48c3a811da1974 to your computer and use it in GitHub Desktop.
minimal voronoi tree
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
)
@harnagpal
Copy link

Hi, I am able to install flametree but not able to install 'voronoise'.

devtools::install_github("djnavarro/voronoise")
Downloading GitHub repo djnavarro/voronoise@master

Installing package into ‘C:/Users/Mithi/Documents/R/win-library/3.6’
(as ‘lib’ is unspecified)

  • installing source package 'voronoise' ...
    ** using staged installation
    ** R
    ** byte-compile and prepare package for lazy loading
    Error: (converted from warning) package 'ggforce' was built under R version 3.6.2
    Execution halted
    ERROR: lazy loading failed for package 'voronoise'
  • removing 'C:/Users/Mithi/Documents/R/win-library/3.6/voronoise'
    Error: Failed to install 'voronoise' from GitHub:
    (converted from warning) installation of package ‘C:/Users/Mithi/AppData/Local/Temp/Rtmp6FcGiX/file11e4443936ce/voronoise_0.0.0.9000.tar.gz’ had non-zero exit status

@harnagpal
Copy link

Forgot to add, the art you make with data is great.

@djnavarro
Copy link
Author

I think this issue is caused by a weird interaction. It looks like you're running R 3.6 and the version of ggforce (which voronoise depends upon) was built under 3.6.2. Normally that would just throw a warning rather than an error, but install_github() automatically converts warnings to errors (I vaguely recall that there is a good reason for this, but it's frustrating). So I think you should be able to solve the issue by installing ggforce, ggplot2 and dplyr from CRAN (or, more likely, I'm guessing you already have these!), and then installing voronoise from github without dependencies:

install_github("djnavarro/voronoise", dependencies = FALSE)

That said, I'm just guessing!

@harnagpal
Copy link

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.

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