The g2 function generally accepts a data.frame or tibble but can also accept other types of objects, as shown in the graph vignettes where it works on objects of class igraph.

These functions use the to_g2r method internally to obtain an object that the package can process and serialise to JSON, this is generally a tibble at the exception of the edge case. This function is exported so one can observe which variables will be made available to use in asp() for the various figures.

For instance, running to_g2r on an object of class loess returns the following tibble.

data(mpg, package = "ggplot2")

smooth <- loess(hwy ~ displ, data = mpg, span = .3)

to_g2r(smooth) %>% 
  head() %>% 
  knitr::kable()
hwy displ .fitted .resid .se .lower .upper
29 1.8 31.71733 -2.7173342 0.5788917 31.13844 32.29623
29 1.8 31.71733 -2.7173342 0.5788917 31.13844 32.29623
31 2.0 29.48387 1.5161318 0.5951880 28.88868 30.07906
30 2.0 29.48387 0.5161318 0.5951880 28.88868 30.07906
26 2.8 23.52388 2.4761238 0.7294572 22.79442 24.25333
26 2.8 23.52388 2.4761238 0.7294572 22.79442 24.25333

Loess

g2(smooth, asp(displ)) %>% 
  fig_point(asp(y = hwy, shape = "circle")) %>% 
  fig_line(asp(y = .fitted)) %>% 
  fig_ribbon(asp(ymin = .lower, ymax = .upper, shape = "smooth"))

Linear model

fit <- lm(dist ~ speed, data = cars)

g2(fit, asp(speed)) %>% 
  fig_point(asp(y = dist, shape = "circle")) %>% 
  fig_line(asp(y = .fitted)) %>% 
  fig_ribbon(asp(ymin = .lower, ymax = .upper, shape = "smooth"))

Ts

g2(AirPassengers, asp(x, y)) %>% 
  fig_line() %>% 
  fig_area()

Mts

deaths <- cbind(ldeaths, fdeaths, mdeaths)

g2(deaths, asp(x)) %>% 
  fig_line(asp(y = ldeaths, color = "#e63946")) %>% 
  fig_line(asp(y = fdeaths, color = "#a8dadc")) %>%
  fig_line(asp(y = mdeaths, color = "#1d3557"))

Xts

library(quantmod)

aapl <- getSymbols("AAPL", env=NULL)

g2(aapl, asp(x, open = AAPL.Open, close = AAPL.Close)) %>% 
  fig_candle(asp(high = AAPL.High, low = AAPL.Low))
library(quantmod)

rate <- getFX("EUR/CHF", auto.assign = FALSE)

g2(rate, asp(x, EUR.CHF)) %>% 
  fig_line()

Forecast

library(forecast)

ts <- USAccDeaths %>%
  stl(s.window='periodic') %>%
  forecast()
 
g2(ts, asp(x)) %>% 
  fig_line(asp(y = y)) %>% 
  fig_line(asp(y = mean)) %>% 
  fig_ribbon(asp(ymin = lower_80, ymax = upper_80)) %>% 
  fig_ribbon(asp(ymin = lower_95, ymax = upper_95))

Survival

library(survival)

data(veteran)

km_fit <- survfit(Surv(time, status) ~ trt, data = veteran)

g2(km_fit, asp(time, color = "strata", shape = "vh")) %>% 
  fig_line(asp(y = estimate)) %>% 
  fig_ribbon(asp(ymin = conf.low, ymax = conf.high)) %>% 
  fig_point(asp(y = n.censor.y, shape = "circle"), stroke = "black")

Acf

cc <- acf(lh, plot = FALSE)

g2(cc) %>% 
  fig_interval(asp(lag, acf, size = 2))

Matrices

correl_mat <- cor(mtcars)

g2(correl_mat) %>% 
  fig_polygon(asp(Var1, Var2, color = Freq)) %>% 
  gauge_color(c("#fae0e4", "#ff85a1", "#ff0a54"))
g2(volcano) %>% 
  fig_polygon(asp(Var1, Var2, color = Freq)) %>% 
  gauge_color(c("#ffba08", "#dc2f02", "#03071e"))