Skip to content

Instantly share code, notes, and snippets.

@mtaylor-semo
Last active February 3, 2019 00:20
Show Gist options
  • Save mtaylor-semo/80a54bb19137a5b25b7b61ad9e0761d0 to your computer and use it in GitHub Desktop.
Save mtaylor-semo/80a54bb19137a5b25b7b61ad9e0761d0 to your computer and use it in GitHub Desktop.
Contemplating the meaning of life
# Based on https://chichacha.netlify.com/2019/01/29/playing-around-with-phyllotactic-spirals/
library(tidyverse) ## for pretty much everything...
phi <- (1 + sqrt(5)) / 2
golden_angle <- pi*(3-sqrt(5))
create_art <- function(n=1800,u=5,v=0,angle=golden_angle,colors="#ffffff",...){
my_colours <- colors ## default is using one colour, white, but I can use vector of colours too.
## Create data frame first using those parameters specified above
df <- tibble(
idx = c(0:(n-1)), ## you can increase the number here to use more lines.
t = seq(0,2*pi,length.out=n), ## since I used 0 to 1800 above, need to add 1
r = sqrt(idx), ## radius
x = r*cos(angle*idx),
y = r*sin(angle*idx),
color_angle = atan2(y=y,x=x) ## get angle between x-axos and the vector from the origin to x,y
)
## In case you specified m2>m then change
v <- ifelse(v<u,v,v%%u)
max_r <- max(df$r)*1.1
my_art <- df %>%
ggplot(aes(x=x,y=y,color=color_angle)) +
geom_point(data= . %>% filter(idx%%u==v), ## Changed from geom_path
aes(size=idx, alpha=idx)) +
coord_fixed() +
theme_void() +
scale_alpha_continuous(guide="none", range=c(0,1), trans="sqrt")+
scale_size_continuous(guide="none", range=c(10,0), trans="sqrt") +
scale_color_gradientn(guide="none",
colors=my_colours) +
theme(panel.background = element_rect(fill="#000000")) +
expand_limits(x=c(-max_r,max_r),y=c(-max_r,max_r))
my_art + annotate(geom="text", x=Inf,y=-Inf,
label=str_glue('n: {n} | u: {u} | v: {v} | angle: {round(angle,3)} radian'),
hjust=1,vjust=-1)
}
p1_colors <- viridis::viridis_pal(alpha = 0.5, begin = 1, end = 0, option = "magma")(20)
p2_colors <- viridis::viridis_pal(alpha = 0.5, option = "viridis")(20)
p1 <- create_art(n=420, angle = 1-cos(42), colors = p1_colors)
p2 <- create_art(n=4200, angle = 1+sin(4.2), colors = p2_colors) +
theme(panel.background = element_blank())
p1 + annotation_custom(grob = ggplotGrob(p2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment