Plotting the Mandelbrot set
# Load packages
pacman::p_load(pacman,tidyverse,GGally,ggthemes,ggplot2,ggvis,httr,plotly,rio,rmarkdown,shiny,rgl)
# Parameters
x_range <- seq(-2, 0.7, length.out = 2000) # adjust the resolution by changing length.out
y_range <- seq(-1.2, 1.2, length.out = 2000)
max_iter <- 100
# Create a blank matrix to hold escape times
escape_times <- matrix(0, nrow = length(y_range), ncol = length(x_range))
# Define the Mandelbrot set
for (i in 1:length(x_range)) {
for (j in 1:length(y_range)) {
z <- 0
c <- complex(real = x_range[i], imaginary = y_range[j])
iter <- 0
while (Mod(z) <= 2 && iter < max_iter) {
z <- z^2 + c
iter <- iter + 1
}
escape_times[j, i] <- iter
}
}
# Colour mapping
colours <- colorRampPalette(c("red", "orange", "yellow", "green", "blue", "purple"))(max_iter)
# Plot points
image(x_range, y_range, escape_times, col = colours, axes = FALSE)
axis(1, at = c(-2, -1, 0, 0.5, 0.7), labels = c("-2", "-1", "0", "0.5", "0.7"))
axis(2, at = c(-1.2, -0.6, 0, 0.6, 1.2), labels = c("-1.2", "-0.6", "0", "0.6", "1.2"))
title("Mandelbrot Set")
