# set-up library
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.4
# set plot image
options(repr.plot.width = 4, repr.plot.height = 4)
# Create circle data plot
t <- seq(0, 2*pi, length.out = 50)
x <- sin(t)
y <- cos(t)
df <- data.frame(t, x, y)
# Make a scatter plot of points in a circle
p <- ggplot(data = df, aes(x, y))
p + geom_point()
# Define the number of points
points <- 500
# Define the Golden Angle
angle <- pi*(3-sqrt(5))
t <- (1:points) * angle
x <- sin(t)
y <-cos(t)
df <- data.frame(t, x, y)
# Make a scatter plot of points in a spiral
p <- ggplot(df, aes(x*t, y*t))
p + geom_point()
df <- data.frame(t, x, y)
# Make a scatter plot of points in a spiral and remove some plot components
p <- ggplot(df, aes(x*t, y*t))
p + geom_point() +
# .... YOUR CODE FOR TASK 4 ....
theme(panel.background = element_rect(fill = "white"),
axis.ticks = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank(),
axis.text = element_blank())
#transpose angle
angle <- t(pi*(3-sqrt(5)))
df <- data.frame(t, x, y)
# Make a scatter plot of points in a spiral and remove some plot components
p <- ggplot(df, aes(x*t, y*t))
p + geom_point() +
# .... YOUR CODE FOR TASK 4 ....
theme(panel.background = element_rect(fill = "white"),
axis.ticks = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank(),
axis.text = element_blank())
# Change the code from Task 4 to modify the
# size, transparency, and color of the points
p <- ggplot(df, aes(x*t, y*t))
p + geom_point(size = 8, alpha = 0.5, color = "#006400") +
theme(panel.background = element_rect(fill = "white"),
panel.grid = element_blank(),
line = element_blank(),
title = element_blank(),
text = element_blank())
Until now, all points have the same appearance (size, color, shape, and alpha). Sometimes we will want to make the appearance of the points dependent on a variable in the dataset. Now we will make the size variable. We will also change the shape of the points. Although we won’t be able to blow on it, the resulting image should remind you of a dandelion.
p <- ggplot(df, aes(x*t, y*t))
p + geom_point(aes(size = t), alpha = 0.5, shape = 8 ) +
theme(panel.background = element_rect(fill = "white"),
panel.grid = element_blank(),
line = element_blank(),
title = element_blank(),
text = element_blank(),
legend.position = "none")
Plants not only use the Golden Angle to arrange leaves. The Golden Angle is also found in the arrangement of sunflower seeds. We don’t need anything new to draw a sunflower; we just need to combine some of the things we already know.
# Define the number of points
points <- 500
# Define the Golden Angle
angle <- pi*(3-sqrt(5))
t <- (1:points) * angle
x <- sin(t)
y <-cos(t)
df <- data.frame(t, x, y)
# shape of the points, and the background color
p <- ggplot(df, aes(x*t, y*t))
p + geom_point(aes(size = t), alpha = 0.5, shape = 17, color = "yellow") +
theme(panel.background = element_rect(fill = "darkmagenta"),
panel.grid = element_blank(),
line = element_blank(),
title = element_blank(),
text = element_blank(),
legend.position = "none")
These patterns are very sensitive to the angle between the points that form the spiral. Small changes to the angle can generate very different images. Let’s look at an example of that.
# Change the value of the angle
angle <- 2.0
points <- 1000
t <- (1:points)*angle
x <- sin(t)
y <- cos(t)
df <- data.frame(t, x, y)
# Copy the plotting code from Task 7
p <- ggplot(df, aes(x*t, y*t))
p + geom_point(aes(size = t), alpha = 0.5, shape = 17, color = "yellow") +
theme(panel.background = element_rect(fill = "darkmagenta"),
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
legend.position = "none")
# Change the values of angle and points
angle <- 13*pi/180
points <- 2000
t <- (1:points)*angle
x <- sin(t)
y <- cos(t)
df <- data.frame(t, x, y)
# Adjust the plot parameters to create the magenta flower
p <- ggplot(df, aes(x*t, y*t))
p + geom_point(size = 80, alpha = 0.1, shape = 1, color = "magenta4")+
theme(legend.position="none",
panel.background = element_rect(fill = "white"),
panel.grid=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank(),
axis.text=element_blank())