Skip to content

Instantly share code, notes, and snippets.

@chichacha
Created January 14, 2019 07:21
Show Gist options
  • Save chichacha/5191b2207078773e044507c2fe6420eb to your computer and use it in GitHub Desktop.
Save chichacha/5191b2207078773e044507c2fe6420eb to your computer and use it in GitHub Desktop.
Experimenting with geom_spoke in ggplot2
library(tidyverse)
base_grid <- tibble(
x = c(1:20),
y = c(1:20)
) %>% expand.grid
my_grid <- base_grid %>%
mutate(color=sample(c("#CFF09E","#A8DBA8","#79BD9A","#3B8686","#0B486B"),
replace=T, size=nrow(my_grid)),
angle = sample(seq(pi/4,2*pi,by=pi/2),
replace=T, size=nrow(my_grid))) %>%
mutate(xend=x+sqrt(2)*cos(angle),
yend=y+sqrt(2)*sin(angle))
my_grid2 <- base_grid %>%
mutate(color=sample(c("#CFF09E","#A8DBA8","#79BD9A","#3B8686","#0B486B"),
replace=T, size=nrow(my_grid)),
angle = sample(seq(pi/2,2*pi,by=pi/2),
replace=T, size=nrow(my_grid))) %>%
mutate(xend=x+cos(angle),
yend=y+sin(angle))
#
#my_grid %>% count(angle) %>% mutate(degree=180*angle/pi)
a <- my_grid %>%
sample() %>%
ggplot(aes(x=x,y=y, color=color)) +
geom_segment(aes(xend=xend,yend=yend, size=1), lineend="round") +
geom_point(size=5) +
scale_color_identity() +
scale_size_identity() +
xlim(c(1,20)) +
ylim(c(1,20)) +
coord_fixed() +
theme_void()
b <- my_grid %>%
sample() %>%
ggplot(aes(x=x,y=y,color=color)) +
geom_spoke(aes(radius=sqrt(2), angle=angle, alpha=x*y), lineend="round", size=5) +
geom_spoke(aes(radius=sqrt(2), angle=angle+pi, alpha=x*y), lineend="round", size=5) +
geom_spoke(aes(radius=sqrt(2), angle=angle+pi/2, alpha=-x*y), lineend="round", size=5,
data=.%>% sample_n(size=100)) +
geom_spoke(aes(radius=sqrt(2), angle=angle-pi/2, alpha=-x*y), lineend="round", size=5,
data=.%>% sample_n(size=100)) +
geom_point(color="#ffffffae") +
scale_color_identity() +
coord_fixed() +
theme_void() +
scale_alpha_continuous(range=c(0.5,0.9), guide="none") +
xlim(c(1,20)) + ylim(c(1,20))
c <- my_grid %>%
sample() %>%
ggplot(aes(x=x,y=y,color=color)) +
geom_spoke(radius=sqrt(2), angle=pi/4, size=3, alpha=0.9) +
geom_spoke(radius=sqrt(2), angle=-pi/4, alpha=0.8, size=3) +
scale_color_identity() +
coord_fixed() +
theme_void() +
scale_alpha_continuous(range=c(0.5,0.9), guide="none") +
xlim(c(1,20)) + ylim(c(1,20))
library(patchwork)
a+b+c
ggsave("geom_spoke_fun.png", width=27, height=9)
@chichacha
Copy link
Author

geom_spoke_fun

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