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.
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()
cc <- acf(lh, plot = FALSE)
g2(cc) %>%
fig_interval(asp(lag, acf, shape = "line"))
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"))
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)
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")
)
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"
)