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

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)