Draw a Rectangle
Overview
Teaching: 0 min
Exercises: 0 minQuestions
How do I draw a rectangle?
Objectives
Create rectangles using ggplot2.
Set aesthetics (e.g. color, opacity) of drawn object.
library(ggplot2)
ggplot() +
geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15))
ggplot() +
geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15),
fill = "blue")
Warning
ggplot() +
geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15, fill = "blue"))
Your Favorite Color
ggplot() + geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15), fill = "purple")
ggplot() +
geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15),
fill = "blue", alpha = 0.4)
Higher alpha is more opaque (less transparent).
ggplot() +
geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15),
fill = "blue", alpha = 0.4, color = "black")
ggplot() +
geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15),
fill = "blue", alpha = 0.4, color = "black") +
theme_void()
ggplot() +
geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15),
fill = "blue", alpha = 0.4, color = "black") +
geom_rect(aes(xmin = 2, xmax = 5, ymin = 3, ymax = 10),
fill = "orange", alpha = 1, color = "black") +
theme_void()
Possible to add each rectangle with a separate line of code (i.e. a separate
call to the geom_rect()
function), but labor intensive and repetitive.
Instead, we can create a table-like data structure to hold the values we want
to use for each rectangle and iterate through each row of that table with
a single line of code.
Key Points
First key point. Brief Answer to questions. (FIXME)
Draw Many Rectangles
Overview
Teaching: 0 min
Exercises: 0 minQuestions
How can I draw many rectangles without a lot of code?
Objectives
Create a dataframe to store values (e.g. color) for each rectangle.
Figure of dataframe structure from another lesson.
You can make a dataframe by manually entering the values you want to use for each variable for each rectangle and then bind them together.
rect1 <- c(xmin = 1, xmax = 3, ymin = 10, ymax = 15, fill = "blue", alpha = 0.4, color = "black")
rect2 <- c(xmin = 2, xmax = 5, ymin = 3, ymax = 10, fill = "orange", alpha = 1, color = "black")
my_rectangles <- rbind(rect1, rect2)
This is also really repetitive and annoying if you have many rectangles. Instead of specifying all of the variable names, you can do this:
rect1 <- c(1, 3, 10, 15, "blue", 0.4, "black")
rect2 <- c(2, 5, 3, 10, "orange", 1, "black")
my_rectangles <- rbind(rect1, rect2)
colnames(my_rectangles) <- c("xmin", "xmax", "ymin", "ymax", "fill", "alpha", "color")
Note to self: Neither of the above actually creates dataframes, so move intro of dataframe terminology to later.
Exercise
Add two more rectangles to your data table.
rect3 <- c(8, 15, 3, 10, "darkblue", 0.3, "black") rect4 <- c(1, 4, 6, 12, "purple", 0.5, "black") my_rectangles <- rbind(my_rectangles, rect3, rect4)
my_rectangles <- as.data.frame(my_rectangles)
ggplot() +
geom_rect(my_rectangles, mapping =
aes(xmin = xmin, xmax = xmax,
ymin = ymin, ymax = ymax),
fill = my_rectangles$fill,
alpha = my_rectangles$alpha) +
theme_void()
Error in grDevices::rgb(col[1, ], col[2, ], col[3, ], alpha): alpha level 2, not in [0,1]
Key Points
First key point. Brief Answer to questions. (FIXME)
Draw a Rectangle
Overview
Teaching: 0 min
Exercises: 0 minQuestions
How do I draw a rectangle?
Objectives
Create rectangles using ggplot2.
Set aesthetics (e.g. color, opacity) of drawn object.
Key Points
First key point. Brief Answer to questions. (FIXME)
Draw Many Rectangles
Overview
Teaching: 0 min
Exercises: 0 minQuestions
How can I draw many rectangles without a lot of code?
Objectives
Create a dataframe to store values (e.g. color) for each rectangle.
Key Points
First key point. Brief Answer to questions. (FIXME)