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.

Note that some of the charts produced can also be made using qg2, see the quick plots vignette.

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
33 1.6 32.35096 0.649043 1.3141041 31.03685 33.66506
32 1.6 32.35096 -0.350957 1.3141041 31.03685 33.66506
32 1.6 32.35096 -0.350957 1.3141041 31.03685 33.66506
29 1.6 32.35096 -3.350957 1.3141041 31.03685 33.66506
32 1.6 32.35096 -0.350957 1.3141041 31.03685 33.66506
29 1.8 31.71733 -2.717334 0.5788917 31.13844 32.29623

Variables in the table able above are therefore available as asp(); see chart below.

Loess

g2(smooth, asp(displ)) %>% 
  fig_point(asp(y = hwy, shape = "circle")) %>% 
  fig_line(asp(y = .fitted, shape = "smooth")) %>% 
  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")) %>% 
  tooltip(shared = TRUE)
deaths <- rbind(ldeaths, fdeaths, mdeaths)

g2(deaths, asp(Var2, Freq, color = Var1)) %>% 
  fig_area() %>% 
  planes(~Var1, rows = 3, cols = 1, type = "list", padding = 20) %>% 
  motif(padding = 30) %>% 
  tooltip(shared = TRUE)

Xts

library(quantmod)

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

aapl %>%
  head(100) %>% 
  g2(asp(x, open = AAPL.Open, close = AAPL.Close)) %>% 
  fig_candle(asp(high = AAPL.High, low = AAPL.Low)) %>% 
  slider(start = 0.5, end = .8)
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),
    alias = "80"
  ) %>% 
  fig_ribbon(
    asp(ymin = lower_95, ymax = upper_95),
    alias = "90"
  )

Survival

library(survival)

km_fit <- survfit(Surv(time, status) ~ trt, data = survival::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, shape = "line"))

Matrices

correl_mat <- cor(mtcars)

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

Roc Curve

library(yardstick)

data(hpc_cv)

hpc_cv %>%
  dplyr::group_by(Resample) %>%
  roc_curve(obs, VF:L) %>% 
  dplyr::mutate(specificity = 1 - specificity) %>% 
  g2(asp(specificity, sensitivity, color = Resample)) %>% 
  fig_path() %>% 
  gauge_y_linear(min = 0, max = 1) %>% 
  planes(
    ~.level, 
    type = "list", 
    cols = 2, 
    rows = 2,
    padding = 25
  ) %>% 
  tooltip(shared = TRUE)

Boxplot + Jitter

df <- tidyr::pivot_longer(iris, -Species) 

g2(df, asp(name, value, color = Species)) %>%  
  fig_boxplot(adjust("dodge")) %>% 
  fig_point(
    asp(shape = "circle"),
    fillOpacity = .5,
    stroke = 0,
    adjust("dodge"), adjust("jitter")
  )

Stl

decomposed <- stl(mdeaths, "per")

g2(decomposed, asp(x, value, color = variable)) %>% 
  fig_line() %>% 
  planes(~variable, cols = 1, rows = 4, type = "list", padding = 20) %>% 
  motif(padding = c(10, 10, 30, 30))

T SNE

library(Rtsne)

# remove uniques
iris_unique <- unique(iris)

set.seed(42L)
tsne <- Rtsne(as.matrix(iris_unique[,1:4]))

df <- as.data.frame(tsne$Y)
df$specie <- iris_unique$Species

g2(df, asp(V1, V2, color = specie)) %>% 
  fig_point(asp(shape = "circle"))

Missing data

Similar to naniar geom_miss_point.

miss <- to_g2r(airquality) %>% 
  dplyr::filter(is.na(Ozone) | is.na(Solar.R))

g2(airquality, asp(Ozone, Solar.R)) %>% 
  fig_point() %>% 
  fig_rug(
    asp(shape = "circle"), 
    fill = "#ff7d7d", 
    fillOpacity = .5,
    stroke = 0,
    data = miss
  ) %>% 
  fig_rug(
    asp(shape = "circle"), 
    fill = "#ff7d7d",
    fillOpacity = .5, 
    stroke = 0,
    data = miss, 
    axis  = "y"
  )