I recently found an interesting tutorial on how to use ggplot2 in R to create pretty graphs. The key is to define a mathematical function that plots points in some sort of pattern and then use various ggplot2 features to make that plot look nice. Find the tutorial at this link.

The tutorial used the concept of phyllotaxis to create interesting scatter plots. Phyllotaxis is the spiral arrangement of the leaves on a stem and that arrangement can be simulated with a bit of mathematics. Here are two plots I made using the phyllotaxis concept.

# Load ggplot2
library(ggplot2)

# Define the number of points
points <- 1000

# Defining the angle between each point
# The 'Golden Angle' is found in nature, about 2.4
#angle <- pi*(3 - sqrt(5)) # About 2.4
angle <- 3.5 # Tried various angles other than the Golden Angle

# Transparancy for the points (0-1.0)
ptAlpha <- 0.25

# Shape of the points (see http://www.cookbook-r.com/Graphs/Shapes_and_line_types/)
ptShape <- 19

# Set the background color
bgColor <- 'lightyellow'

# Create a df with the points to plot
t <- (1:points) * angle
x <- sin(t)
y <-cos(t)
df <- data.frame(t, x, y)

ggplot(df, aes(x*t, y*t)) +
geom_point(aes(color = t), size = 10.0, alpha = ptAlpha, shape = ptShape) +
scale_color_gradient(low = 'orangered1', high = 'gold2') +
theme(panel.background = element_rect(fill = bgColor),
panel.grid = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.position = "none")

# These lines are explained in the first image

library(ggplot2)
points <- 500
angle <- 7.0
ptColor <- 'darkgreen'
ptAlpha <- 0.5
ptShape <- 8
bgColor <- '#eeffee'

t <- (1:points) * angle
x <- sin(t)
y <-cos(t)
df <- data.frame(t, x, y)

ggplot(df, aes(x*t, y*t)) +
geom_point(aes(size = t, color = t), alpha = ptAlpha, shape = ptShape) +
scale_color_gradient(low = 'blue4', high = 'green4') +
theme(panel.background = element_rect(fill = bgColor),
panel.grid = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.position = "none")

The mathematics for these images was fairly simple and it took me longer to figure out all of the ggplot2 settings needed to make the plots look nice without all of the analysis trappings (like gridlines). I had a couple of hours of fun creating this post — now, back to work…