Skip to contents

Setup

Packages

Base Plot

To avoid repetitive code, we make a base plot:


my_font <- "Roboto Condensed"
my_font_size <- 20
my_point_size <- 2
my_arrowhead <- arrowheadr::arrow_head_deltoid(2.1)

# my_colors <- viridis::viridis(2, begin = .25, end = .5)
my_colors <- c("#3B528B", "#21908C")

theme_set(
  theme_minimal(
    base_size = my_font_size,
    base_family = my_font) +
    theme(axis.title.y = element_text(angle = 0, vjust = 0.5)))

bp <- ggdiagram(
  font_family = my_font,
  font_size = my_font_size,
  point_size = my_point_size,
  linewidth = .5,
  theme_function = theme_minimal,
  axis.title.x =  element_text(face = "italic"),
  axis.title.y = element_text(
    face = "italic",
    angle = 0,
    hjust = .5,
    vjust = .5)) +
  scale_x_continuous(labels = WJSmisc::signs_centered,
                     limits = c(-4, 4)) +
  scale_y_continuous(labels = signs::signs,
                     limits = c(-4, 4))

Polygons

The ob_polygon function creates an object that connects points to make a polygon.

p <- ob_point(x = c(-4,-3, 4,  3, -1), 
           y = c( 0, 4, 3, -3, -4))
bp + 
  ob_polygon(p, fill = "dodgerblue3")
Figure 1: Plotting a path.

Polygon Labels

The label of a ob_polygon object is placed, by default, at the centroid of the polygon. The centroid is the point whose x coordinate is the average of all the x coordinates of the polygon’s points and whose y coordinate is likewise the average of all the point’s y coordinates.

bp +
  ob_polygon(
    p = p,
    label = ob_label(
      "My<br>Polygon",
      size = 20,
      color = "white"
    ),
    fill = "dodgerblue4"
  )
Figure 2: A path with a curved label

Connecting polygons

Connections between polygons are arrows that emanate from and point towards the polygons’ centroids.

bp + 
  {p1 <- ob_polygon(.5 * p - ob_point(1.9,1.9), 
              fill = "dodgerblue4")} + 
  {p2 <- ob_polygon(.5 * p + ob_point(1.9, 1.9), 
              fill = "orchid4")} + 
  connect(p1, p2, 
          resect = 1, 
          label = ".45")
Figure 3: Arrow between two polygons

Rounding polygons

The @radius property controls the radius of the rounded vertices. It must be of length 1. It can be given in as a ggplot2::unit or as a numeric value. If numeric, it is understood as a proportion of the plot area width.

bp + 
  ob_polygon(
    p, 
    radius = unit(5, "mm"),
    fill = "dodgerblue4")
Figure 4: A polygon with rounded vertices