This lesson is still being designed and assembled (Pre-Alpha version)

R for Artists

Draw a Rectangle

Overview

Teaching: 0 min
Exercises: 0 min
Questions
  • 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))

plot of chunk first-rect

ggplot() + 
  geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15), 
            fill = "blue")

plot of chunk add-fill

Warning

ggplot() + 
  geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15, fill = "blue"))

plot of chunk fill-in-aes

Your Favorite Color

ggplot() + 
geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15), fill = "purple")

plot of chunk fav-color

ggplot() + 
  geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15), 
            fill = "blue", alpha = 0.4)

plot of chunk add-transparency 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")

plot of chunk add-outline

ggplot() + 
  geom_rect(aes(xmin = 1, xmax = 3, ymin = 10, ymax = 15), 
            fill = "blue", alpha = 0.4, color = "black") + 
  theme_void()

plot of chunk blank-canvas

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()

plot of chunk two-rectangles 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 min
Questions
  • 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]

plot of chunk rects-from-dataframe

Key Points

  • First key point. Brief Answer to questions. (FIXME)


Draw a Rectangle

Overview

Teaching: 0 min
Exercises: 0 min
Questions
  • 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 min
Questions
  • 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)