The info_* family of functions allows annotating visualisations.

Marker

One can use data to place informational annotations.

library(g2r)

data(penguins, package = 'palmerpenguins')

samp <- penguins[sample(nrow(penguins), 10), ]
samp$cnt <- letters[1:10]

g2(penguins, asp(body_mass_g, flipper_length_mm)) %>% 
  fig_point(asp(color = sex)) %>% 
  info_marker(
    asp(body_mass_g, flipper_length_mm, content = cnt), 
    top = TRUE,
    data = samp
  )

Horizontal & Vertical Lines

There are convenience functions to draw vertical and horizontal lines.

g2(cars, asp(speed, dist)) %>% 
  fig_point() %>% 
  info_hline(asp(y = 43, content = "Mean Distance")) %>% 
  info_vline(asp(x = 15, content = "Mean Speed"))

Otherwise lines can be specified by defining the start (x, y) and end (xend, yend) of the line.

g2(cars, asp(speed, dist)) %>% 
  fig_point() %>% 
  info_line(
    asp(
      x = 0, y = 0, 
      xend = "max", yend = "max",
      content = "A diagonal line"
    )
  )

Types

There are a number of informational annotations:

  • text
  • image
  • arc
  • line
  • region
  • region_filter
  • data_marker
  • data_region
  • shape
  • html

Region filter

The area and line charts are actually white (asp(color = "white")), the color is actually applied with the “region filter.”

df <- data.frame(
  x = 1:100,
  y = runif(100, -5, 5)
)

g2(df, asp(x, y, color = "white", shape = "smooth")) %>% 
  fig_area() %>% 
  fig_line() %>% 
  info_region_filter(
    asp(
      x = "min", y = 0, 
      xend = "max", yend = "min"
    ), 
    top = TRUE,
    color = "red"
  ) %>% 
  info_region_filter(
    asp(
      x = "min", y = "max", 
      xend = "max", yend = 0
    ), 
    top = TRUE,
    color = "green"
  ) %>% 
  gauge_y_linear(min = -7, max = 7)

Text

Positional aspects (x, y, xend, yend) accept column names, string, integers and the following key terms: min, max, median, start, end.

g2(cars, asp(speed, dist)) %>% 
  fig_point() %>% 
  info_text(
    asp(x = "median", y = "median"),
    content = "Median"
  )

Region

Highlight a region.

g2(cars, asp(speed, dist)) %>% 
  fig_point() %>% 
  info_region(
    asp(
      x = 10, y = "max",
      xend = 20, yend = "min"
    )
  )

Data Region

data("economics", package = "ggplot2")

g2(economics, asp(date, unemploy)) %>% 
  fig_line() %>% 
  info_data_region(
    asp(
      x = "1989-03-01", y = 6205,
      xend = "1992-06-01", yend = 10040,
      content = "Data Region"
    )
  )