Skip to content

Instantly share code, notes, and snippets.

@chichacha
Created January 9, 2019 07:04
Show Gist options
  • Save chichacha/01264b338ec2006bc81692c2f8cbec74 to your computer and use it in GitHub Desktop.
Save chichacha/01264b338ec2006bc81692c2f8cbec74 to your computer and use it in GitHub Desktop.
Drawing Flower-Like Object with ggplot2
library(tidyverse)
flower_vertices <- function(xc,yc,radius,k=5,offset=0,d=pi*(3-sqrt(5)),npoints=360,...){
i = seq.int(0,npoints,by=1)
t = seq(0,2*pi, length.out=npoints+1)
tend = seq(0-d,2*pi-d, length.out=npoints+1)
m = sqrt(radius)*0.5 * cos(k * t) + offset
x = xc + m * cos(t)
y = yc + m * sin(t)
xend = xc + m * cos(tend)
yend = yc + m * sin(tend)
df <- tibble(i=i,t=t,x=x,y=y,xend,yend,r=m) %>% filter(i!=0)
return(df)
}
col_pal <- ggthemes::tableau_color_pal(palette="Hue Circle")(19)
flower_vertices(0,0,1,k=10,offset=0.1)
tibble(
xc=rep(c(1:10),time=5),
yc=rep(c(1:5),each=10),
radius=1,
k = rep(c(2:26), each=2),
offset = 0,
d = rep(c(pi*(3-sqrt(5)),sqrt(5)), times=25),
id=c(1:50)
)%>% mutate(v=pmap(.,flower_vertices)) %>%
unnest(v) %>%
ggplot(aes(x=x,y=y, group=id)) +
geom_polygon(fill=sample(str_c(col_pal,"70"),size=1)) +
geom_polygon(aes(x=xend,y=yend),fill=sample(str_c(col_pal,"70"),size=1)) +
geom_segment(aes(xend=xend, yend=yend), size=0.05, color="#ffffffae") +
coord_fixed() +
theme_void() +
theme(panel.background=element_rect(fill="#000000de"))
@chichacha
Copy link
Author

flowers_experimental_small

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