Created
January 5, 2019 23:08
-
-
Save chichacha/6d62cc793b3a55203c70a3d118f11ad0 to your computer and use it in GitHub Desktop.
Drawing Half Circles & Heart & Cannabis
This file contains hidden or 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(tidyverse) | |
cannabis_vertices <- function(xc,yc,radius,npoints=1000,...){ | |
radius=radius/100 | |
t = seq(-pi,pi, length.out=npoints+1) | |
m = (1+.9*cos(8*t)) * (1+.1*cos(24*t)) * (.9+0.5*cos(200*t)) * (1+sin(t)) | |
x = xc + m*radius * cos(t) | |
y = yc + m*radius * sin(t) | |
df <- tibble(x=x,y=y) | |
return(df) | |
} | |
## Drawing One Cannabis | |
cannabis_vertices(0,0,1,1000) %>% | |
ggplot(aes(x=x,y=y)) + | |
geom_polygon(fill="#2E4347") + ## Dark Green Colour | |
coord_fixed() + | |
theme_void() | |
## Drawing multiple of them | |
tibble( | |
xc=c(1,2,3,4,5), | |
yc=c(1,1,1,1,1), | |
radius = c(10,20,25,20,10), | |
id =c(1,2,3,4,5) | |
) %>% mutate(vertices = pmap(.,cannabis_vertices)) %>% | |
unnest(vertices) %>% | |
ggplot(aes(x=x,y=y,group=id)) + | |
geom_polygon(fill="#4A5F67ae") + | |
theme_void() + | |
coord_fixed() | |
This file contains hidden or 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
half_circle_vertices <- function(xc,yc,radius,direction=1,npoints=25,...){ | |
radius=radius/2 | |
t = seq(0,pi,length.out=npoints+1) * direction ## instead of -pi to pi, just draw from 0 to pi! | |
x = xc + radius * cos(t) | |
y = yc + radius * sin(t) | |
df <- tibble(x=x,y=y) | |
} | |
half_circle_vertices(0,0,1,npoints=100) %>% | |
ggplot(aes(x=x,y=y)) + | |
geom_polygon() + | |
coord_fixed() + | |
theme_void() + | |
annotate(geom="text", x=0,y=0.25,family="Avenir", | |
label="Hello\nI'm Half Circle", size=20, color="#ffffffde") | |
tibble(xc=sample(1:100),yc=sample(1:20,size=100,replace=T), | |
radius=rbeta(100,3,1)*10,direction=sample(c(-1,1),size=100,replace=T), | |
id=c(1:100)) %>% | |
mutate(vertices=pmap(.,half_circle_vertices)) %>% | |
unnest(vertices) %>% | |
ggplot(aes(x=x,y=y,group=id)) + | |
geom_polygon(aes(fill=id%%5)) + | |
theme_void() + | |
scale_fill_viridis_c(option="magma", end=0.9, guide="none", alpha=0.8) + | |
coord_fixed() | |
This file contains hidden or 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
## How about drawing heart! | |
heart_vertices <- function(xc,yc,radius,npoints=100,...){ | |
radius = radius*0.05 | |
yc = yc + 0.1*radius | |
t = seq(-pi,pi, length.out=npoints+1) | |
x = xc + 16 * radius*(sin(t))^3 | |
y = yc + radius*13*cos(t) - radius*5*cos(2*t) - radius*2*cos(3*t) - radius*cos(4*t) | |
df <- tibble(x=x,y=y) | |
return(df) | |
} | |
heart_vertices(0,0,10,1000) %>% | |
ggplot(aes(x=x,y=y)) + | |
geom_polygon() + | |
coord_fixed() + | |
scale_y_continuous(breaks=pretty_breaks(n=10)) + | |
scale_x_continuous(breaks=pretty_breaks(n=10)) + | |
theme_void() + | |
annotate(geom="text", label="Hello I'm Heart!", | |
color="#ffffffde",x=0,y=0, size=10, | |
family="Avenir") | |
df <- tibble( | |
xc = rep(c(1:12), times=10), | |
yc = rep(c(1:10), each=12), | |
radius=rbeta(120,3,1), | |
id = c(1:120) | |
) | |
df_gg <- df %>% mutate(vertices=pmap(.,heart_vertices)) %>% | |
unnest(vertices) | |
df_gg %>% | |
ggplot(aes(x=x,y=y,group=id)) + | |
geom_polygon(aes(fill=sample(id)%%4)) + | |
theme_void() + | |
scale_fill_viridis_c(option="magma", end=0.6, guide="none", alpha=0.5) + | |
coord_fixed() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment