suppressPackageStartupMessages({
  library(tidyverse)
  library(scales)
  library(glue)
  library(flametree)
})

1 Simple graphs

Let’s start from drawing a random line using ggplot

tibble(x1 = 1,
       y1 = 5,
       x2 = 6,
       y2 = -2) %>% 
  ggplot()+
  geom_segment(aes(x = x1, 
                   y = y1, 
                   xend = x2, 
                   yend = y2))+
  theme_void()

NA

Let’s do many lines

N <- 30

tibble(x1 = runif(N, -1, 1),
       y1 = runif(N, -1, 1), 
       x2 = runif(N, -1, 1),
       y2 = runif(N, -1, 1)) %>% 
  ggplot()+
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2))+
  theme_void()+
  labs(subtitle = "Lines")

Now let’s add some random color, thickness and opacity

N <- 30
cols <- rainbow(N)
cols <- c("#EF892D", "#FEC784", "#45555F", "#93ADC5", "#BF49DB")
#set.seed(43)

tibble(line = 1:N %% 5,
       x1 = runif(N, -1, 1),
       y1 = runif(N, -1, 1), 
       x2 = runif(N, -1, 1),
       y2 = runif(N, -1, 1), 
       size = runif(N, 0, 3),
       alpha = runif(N, 0.1, 0.9)) %>% 
  ggplot()+
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2, size = size, alpha = alpha, color = as.factor(line)),show.legend = FALSE)+
  scale_color_manual(values = cols)+
  scale_size_continuous(range = c(2,20))+
  theme_void()+
  labs(subtitle = "Rainbow Lines")

2 Using Grid and Trigonometric Functions

Let’s create a simple grid first

seq(-2,2, by = 0.2) %>% 
  expand.grid(x=., y=.) %>% 
  ggplot(aes(x = x, y = y))+
  geom_point(size = 0)+
  theme_bw()

What happens if we switch to polar coordinates

seq(-2,2, by = 0.2) %>% 
  expand.grid(x=., y=.) %>% 
  ggplot(aes(x = x, y = y))+
  geom_point(size = 0)+
  coord_polar()+    # x becomes an angle and y becomes radius
  theme_bw()

We can use more complex trigonometric functions to generate really intricate and beautiful patterns (source)

seq(-2,2, by = 0.01) %>% 
  expand.grid(x=., y=.) %>% 
  ggplot(aes(x = x^3 - sin(y), y = y^3 - cos(x)))+
  geom_point(alpha = 0.05, color = "black", shape = 20, size = 0)+
  theme_void()+
  coord_polar()+
  labs(subtitle = "Dandelion")

seq(-3,3, by = 0.01) %>% 
  expand.grid(x=., y=.) %>% 
  ggplot(aes(x = 1- x-sin(y^2), y = 1 + y - cos(x^2)))+
  geom_point(alpha = 0.05, color = "blue", shape = 20, size = 0)+
  theme_void()+
  coord_polar()+
  labs(subtitle = "Flower")