Title: | Interactive Grammar of Graphics |
---|---|
Description: | Interactive grammar of graphics. |
Authors: | John Coene [aut, cre], Opifex [cph, fnd] |
Maintainer: | John Coene <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.1 |
Built: | 2024-11-14 06:01:56 UTC |
Source: | https://github.com/devOpifex/g2r |
Filter data.
action_filter_data(g, input, asp, operator = ">")
action_filter_data(g, input, asp, operator = ">")
g |
An object of class |
input |
The |
asp |
Aspect (column) to filter. |
operator |
Operator of the filter, this is combined with
the value from the |
# works in Rmarkdown input_slider( "yFilter", "Filter Y >", value = 50, min = 40, max = 70, step = 5 ) ## Not run: g2(cars, asp(speed, dist)) %>% fig_point() %>% fig_smooth() %>% action_filter_data( "yFilter", dist, operator = ">" ) ## End(Not run)
# works in Rmarkdown input_slider( "yFilter", "Filter Y >", value = 50, min = 40, max = 70, step = 5 ) ## Not run: g2(cars, asp(speed, dist)) %>% fig_point() %>% fig_smooth() %>% action_filter_data( "yFilter", dist, operator = ">" ) ## End(Not run)
Select a dataset with an input_select()
.
action_select_data(g, input, datasets)
action_select_data(g, input, datasets)
g |
An object of class |
input |
Id of the |
datasets |
A key value pair |
# works in Rmarkdown input_select( "selector", "Select a dataset", c("Cars", "More Cars") ) cars1 <- cars cars2 <- cars + c(1, -4) g2(cars, asp(dist, speed)) %>% fig_point() %>% action_select_data( "selector", datasets = list( "Cars" = cars1, "More Cars" = cars2 ) )
# works in Rmarkdown input_select( "selector", "Select a dataset", c("Cars", "More Cars") ) cars1 <- cars cars2 <- cars + c(1, -4) g2(cars, asp(dist, speed)) %>% fig_point() %>% action_select_data( "selector", datasets = list( "Cars" = cars1, "More Cars" = cars2 ) )
Toggle the visibily of a chart.
action_toggle_visibility(g, btn)
action_toggle_visibility(g, btn)
g |
An object of class |
btn |
Id of the input_button that toggles the visibility. |
# works in Rmarkdown input_button("toggle", "Show/hide chart") g2(mtcars, asp(qsec, mpg)) %>% fig_point() %>% action_toggle_visibility("toggle")
# works in Rmarkdown input_button("toggle", "Show/hide chart") g2(mtcars, asp(qsec, mpg)) %>% fig_point() %>% action_toggle_visibility("toggle")
Adjust a figure.
adjust(type, margin = NULL, dodge_by = NULL)
adjust(type, margin = NULL, dodge_by = NULL)
type |
A vector of types of adjustement to apply to the figure, see the "types" section below for valid values. |
margin |
Margin, between |
dodge_by |
Bare column name to use as group for dodge. |
Valid values for the type
argument.
stack
dodge
jitter
symmetric
df <- data.frame( x = c(letters, letters), y = runif(52), grp = rep(c("A", "Z"), each = 2) ) g2(df, asp(x, y, color = grp)) %>% fig_interval(adjust("stack"))
df <- data.frame( x = c(letters, letters), y = runif(52), grp = rep(c("A", "Z"), each = 2) ) g2(df, asp(x, y, color = grp)) %>% fig_interval(adjust("stack"))
Aliases an aspect, this changes the name of the aspects when displayed in axis titles, tooltips, labels, and other places.
aka(g, asp, alias)
aka(g, asp, alias)
g |
An object of class |
asp |
Bare name of aspect to alias. |
alias |
A string defining the alias. |
# see tooltip g2(cars, asp(speed, dist)) %>% fig_point() %>% aka(dist, "SO FAR")
# see tooltip g2(cars, asp(speed, dist)) %>% fig_point() %>% aka(dist, "SO FAR")
Helper function to build animations.
enter()
Animation$enter(animation = NULL, easing = NULL, delay = NULL, duration = NULL)
animation
Name of animation; clipIn
, zoomIn
,
pathIn
, scaleInY
, scaleInX
, fanIn
, or fadeIn
.
easing
Name of easing function.
delay, duration
Delay and duration in milliseconds.
Animation to use on enter
leave()
Animation$leave(animation = NULL, easing = NULL, delay = NULL, duration = NULL)
animation
Name of animation; lineWidthOut
,
zoomOut
, pathOut
, or fadeOut
.
easing
Name of easing function.
delay, duration
Delay and duration in milliseconds.
Animation to use on leave
appear()
Animation$appear( animation = NULL, easing = NULL, delay = NULL, duration = NULL )
animation
Name of animation; clipIn
,
zoomIn
, pathIn
, scaleInY
, scaleInX
, fanIn
,
or fadeIn
.
easing
Name of easing function.
delay, duration
Delay and duration in milliseconds.
Animation to use on appear
update()
Animation$update( animation = NULL, easing = NULL, delay = NULL, duration = NULL )
animation
Name of animation; fadeIn
, or fanIn
.
easing
Name of easing function.
delay, duration
Delay and duration in milliseconds.
Animation to use on appear
print()
Animation$print()
retrieve()
Animation$retrieve()
Retrieve the animation list
clone()
The objects of this class are cloneable with this method.
Animation$clone(deep = FALSE)
deep
Whether to make a deep clone.
# create animation anim <- Animation$ new()$ appear( duration = 2000, delay = 500 ) iris %>% g2(asp(Sepal.Length, Sepal.Width, color = Species)) %>% fig_point(anim)
# create animation anim <- Animation$ new()$ appear( duration = 2000, delay = 500 ) iris %>% g2(asp(Sepal.Length, Sepal.Width, color = Species)) %>% fig_point(anim)
Define aspects of a visualisation.
asp(x, y, ...)
asp(x, y, ...)
x , y
|
Defines values to map to cartesian coordinates. |
... |
Any other key value pair of aspect. |
x
, y
ymin
, ymax
size
color
shape
label
tooltip
style
interaction
color
x
, y
start
, end
content
top
Configure the axes.
Customise the legend.
axis_x(g, ...) axis_y(g, ...) axis_asps(g, asps, ...) axis_hide(g) legend_color(g, ...) legend_size(g, ...) legend_asps(g, asps, ...)
axis_x(g, ...) axis_y(g, ...) axis_asps(g, asps, ...) axis_hide(g) legend_color(g, ...) legend_size(g, ...) legend_asps(g, asps, ...)
g |
An object of class |
... |
Options to pass to the legend, pass |
asps |
Aspect (column names) to change the legend. |
axis_x
: Customise the x axis.
axis_y
: Customise the y axis.
axis_asps
: Customise the axis by aspects (column names).
axis_hide
: Hide all axis.
legend_color
: Customise the x axis.
legend_size
: Customise the y axis.
legend_asps
: Customise the axis by aspects (column names).
g <- g2(cars, asp(speed, dist)) %>% fig_point() # hide axis g %>% axis_x(FALSE) # same as above g %>% axis_asps("speed", FALSE) # change position g %>% axis_x(position = "top") g <- g2(mtcars, asp(mpg, qsec, color = gear)) %>% fig_point() g %>% legend_color(position = "top") g %>% legend_color(FALSE)
g <- g2(cars, asp(speed, dist)) %>% fig_point() # hide axis g %>% axis_x(FALSE) # same as above g %>% axis_asps("speed", FALSE) # change position g %>% axis_x(position = "top") g <- g2(mtcars, asp(mpg, qsec, color = gear)) %>% fig_point() g %>% legend_color(position = "top") g %>% legend_color(FALSE)
Add axis titles.
axis_title_x(g, title, ..., fontSize = 10, offset = 30) ## S3 method for class 'g2r' axis_title_x(g, title, ..., fontSize = 10, offset = 30) axis_title_y(g, title, ..., fontSize = 10, offset = 50) ## S3 method for class 'g2r' axis_title_y(g, title, ..., fontSize = 10, offset = 50)
axis_title_x(g, title, ..., fontSize = 10, offset = 30) ## S3 method for class 'g2r' axis_title_x(g, title, ..., fontSize = 10, offset = 30) axis_title_y(g, title, ..., fontSize = 10, offset = 50) ## S3 method for class 'g2r' axis_title_y(g, title, ..., fontSize = 10, offset = 50)
g |
An object of class |
title |
Title to use on the axis. |
... |
Options to customise the title. |
fontSize |
Size of the font of the label. |
offset |
Offset between title and axis,
if |
Capture events in shiny.
capture_event(g, event, callback = NULL, when = c("on", "once", "off"))
capture_event(g, event, callback = NULL, when = c("on", "once", "off"))
g |
An object of class |
event |
Name of event to trigger the callback. |
callback |
A callback function to run when the |
when |
When the event should be triggered. |
The callback
function should accept a single
argument; the event data. If no callback function is passed
(NULL
) then one is generated. The generated callback function
sets a shiny input <chartId>_<eventName>
with the event data.
g2(iris, asp(Sepal.Width, Sepal.Length)) %>% fig_point() %>% capture_event("point:click")
g2(iris, asp(Sepal.Width, Sepal.Length)) %>% fig_point() %>% capture_event("point:click")
Dynamically change the data of a shiny plot.
change_data(g, data)
change_data(g, data)
g |
An object of class |
data |
New dataset to replaced the one used to currently plot the data. |
library(shiny) makeData <- function(){ data.frame( x = runif(100), y = runif(100), size = runif(100) ) } ui <- fluidPage( g2Output("plot"), actionButton("change", "Change data") ) server <- function(input, output){ output$plot <- renderG2({ g2(makeData(), asp(x, y, size = size)) %>% fig_point() }) observeEvent(input$change, { g2_proxy("plot") %>% change_data(makeData()) }) } if(interactive()){ shinyApp(ui, server) }
library(shiny) makeData <- function(){ data.frame( x = runif(100), y = runif(100), size = runif(100) ) } ui <- fluidPage( g2Output("plot"), actionButton("change", "Change data") ) server <- function(input, output){ output$plot <- renderG2({ g2(makeData(), asp(x, y, size = size)) %>% fig_point() }) observeEvent(input$change, { g2_proxy("plot") %>% change_data(makeData()) }) } if(interactive()){ shinyApp(ui, server) }
Configure a figure.
config( id = NULL, visible = NULL, region = NULL, padding = NULL, theme = NULL, ... )
config( id = NULL, visible = NULL, region = NULL, padding = NULL, theme = NULL, ... )
id |
Id of figure. |
visible |
Whether the figure is visible. |
region |
Region that the figure should occupy on the canvas. |
padding |
Padding around the figure. |
theme |
Theme of the figure. |
... |
Any other options from the official documentation. |
mtcars %>% g2(asp(qsec)) %>% fig_point( asp(y = mpg), config( region = list( start = list(x = 0, y = 0), end = list(x = 0.5, y = 1) ) ) ) %>% fig_point( asp(y = wt), config( region = list( start = list(x = 0.5, y = 0), end = list(x = 1, y = 1) ) ) )
mtcars %>% g2(asp(qsec)) %>% fig_point( asp(y = mpg), config( region = list( start = list(x = 0, y = 0), end = list(x = 0.5, y = 1) ) ) ) %>% fig_point( asp(y = wt), config( region = list( start = list(x = 0.5, y = 0), end = list(x = 1, y = 1) ) ) )
Configure chart coordinates axis.
coord_type(g, type = c("rect", "polar", "theta", "helix"), ...) coord_rotate(g, angle = 90) coord_scale(g, x, y) coord_reflect(g, axis = "xy") coord_transpose(g)
coord_type(g, type = c("rect", "polar", "theta", "helix"), ...) coord_rotate(g, angle = 90) coord_scale(g, x, y) coord_reflect(g, axis = "xy") coord_transpose(g)
g |
An object of class |
type |
Type of coordinate axis. |
... |
Any other options. |
angle |
Angle of axis rotation. |
x , y
|
Scale of axis along |
axis |
Axis to reflect (reverse). |
coord_type
: Type of coordinates to use where rect
corresponds to cartesian.
coord_rotate
: Rotate the coordinates by a certain angle.
coord_scale
: Rescale the coordinates.
coord_reflect
: Mirror the axis along the x, y, or xy (both)
axes.
coord_transpose
: x, y axes displacement.
g2(cars, asp(speed, dist, color = dist)) %>% fig_point() %>% coord_type("helix")
g2(cars, asp(speed, dist, color = dist)) %>% fig_point() %>% coord_type("helix")
Customise the crosstalk selection handle.
crosstalk_select(g, attribute, on, off = NULL)
crosstalk_select(g, attribute, on, off = NULL)
g |
An object of class |
attribute |
Attribute to customise, e.g.: |
on , off
|
Value to set the |
Function to use in motif()
and style specific elements.
element( ..., shape = NULL, figure = c("point", "area", "edge", "line", "interval", "polygon", "schema"), state = c("default", "active", "inactive", "selected") ) elementPoint( ..., shape = c("hollow-circle", "cross", "hypen", "line", "plus", "tick", "circle", "square", "bowtie", "diamond", "hexagon", "triangle", "triangle-down", "hollow-square", "hollow-bowtie", "hollow-triangle-down"), state = c("default", "active", "inactive", "selected") ) elementLine( ..., shape = c("line", "dot", "dash", "smooth", "hv", "vh", "hvh", "vhv"), state = c("default", "active", "inactive", "selected") ) elementArea( ..., shape = c("area", "smooth", "line", "smooth-line"), state = c("default", "active", "inactive", "selected") ) elementEdge( ..., shape = c("line", "vhv", "smooth", "arc"), state = c("default", "active", "inactive", "selected") ) elementInterval( ..., shape = c("rect", "hollow-rect", "line", "tick", "funnel", "pyramid"), state = c("default", "active", "inactive", "selected") ) elementPolygon(..., state = c("default", "active", "inactive", "selected")) elementSchema( ..., shape = c("box", "candle"), state = c("default", "active", "inactive", "selected") )
element( ..., shape = NULL, figure = c("point", "area", "edge", "line", "interval", "polygon", "schema"), state = c("default", "active", "inactive", "selected") ) elementPoint( ..., shape = c("hollow-circle", "cross", "hypen", "line", "plus", "tick", "circle", "square", "bowtie", "diamond", "hexagon", "triangle", "triangle-down", "hollow-square", "hollow-bowtie", "hollow-triangle-down"), state = c("default", "active", "inactive", "selected") ) elementLine( ..., shape = c("line", "dot", "dash", "smooth", "hv", "vh", "hvh", "vhv"), state = c("default", "active", "inactive", "selected") ) elementArea( ..., shape = c("area", "smooth", "line", "smooth-line"), state = c("default", "active", "inactive", "selected") ) elementEdge( ..., shape = c("line", "vhv", "smooth", "arc"), state = c("default", "active", "inactive", "selected") ) elementInterval( ..., shape = c("rect", "hollow-rect", "line", "tick", "funnel", "pyramid"), state = c("default", "active", "inactive", "selected") ) elementPolygon(..., state = c("default", "active", "inactive", "selected")) elementSchema( ..., shape = c("box", "candle"), state = c("default", "active", "inactive", "selected") )
... |
Key value pairs to pass to |
shape |
Shape to modify, if |
figure |
Figure to modify. |
state |
State of the shape to modify. |
element()
will work for any figure, but other
functions may be more convienient to use.
element
: Customise any element.
elementPoint
: Customise point.
elementLine
: Customise line.
elementArea
: Customise area.
elementEdge
: Customise edge.
elementInterval
: Customise interval.
elementPolygon
: Customise polygon.
elementSchema
: Customise schema.
g2(iris, asp(Sepal.Width, Sepal.Length)) %>% fig_point( asp(color = Species, shape = "circle") ) %>% motif( brandColor = "orange", backgroundColor = "black", elementPoint( shape = "circle", stroke = "white", fillOpacity = .7 ) )
g2(iris, asp(Sepal.Width, Sepal.Length)) %>% fig_point( asp(color = Species, shape = "circle") ) %>% motif( brandColor = "orange", backgroundColor = "black", elementPoint( shape = "circle", stroke = "white", fillOpacity = .7 ) )
Add an area figure.
fig_area(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_area(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
g2(Orange, asp(age, circumference, color = Tree)) %>% fig_area(adjust("stack"))
g2(Orange, asp(age, circumference, color = Tree)) %>% fig_area(adjust("stack"))
Add a bin figure to the chart.
fig_bin( g, ..., type = c("rectangle", "hexagon"), bins = c(10, 10), size_count = TRUE, sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "count" )
fig_bin( g, ..., type = c("rectangle", "hexagon"), bins = c(10, 10), size_count = TRUE, sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "count" )
g |
An object of class |
... |
Options to pass to the figure, including |
type |
The shape of bin to create. |
bins |
Number of bins by dimension (width, height). |
size_count |
Whether to size the binds by count. |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
alias |
Name of the range to display on tooltips, labels, etc. |
Requires the x
and y
aspects.
g2(cars, asp(speed, dist)) %>% fig_bin(size_count = FALSE) g2(cars, asp(speed, dist)) %>% fig_bin(type = "hexagon")
g2(cars, asp(speed, dist)) %>% fig_bin(size_count = FALSE) g2(cars, asp(speed, dist)) %>% fig_bin(type = "hexagon")
Add a boxplot figure to the chart.
fig_boxplot(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_boxplot(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
# wide to long # tidyr::pivot_longer(iris, -Species) df <- reshape( iris, varying = names(iris)[1:4], direction = "long", v.names = "value", idvar = "Species", new.row.names = 1:600, timevar = "var", times = names(iris)[1:4] ) g2(df, asp(var, value, color = Species)) %>% fig_boxplot(adjust("dodge")) g2(iris, asp(y = Sepal.Length, color = Species)) %>% fig_boxplot(adjust("dodge")) g2(iris, asp(x = Species, y = Sepal.Length, color = Species)) %>% fig_boxplot(adjust("dodge"))
# wide to long # tidyr::pivot_longer(iris, -Species) df <- reshape( iris, varying = names(iris)[1:4], direction = "long", v.names = "value", idvar = "Species", new.row.names = 1:600, timevar = "var", times = names(iris)[1:4] ) g2(df, asp(var, value, color = Species)) %>% fig_boxplot(adjust("dodge")) g2(iris, asp(y = Sepal.Length, color = Species)) %>% fig_boxplot(adjust("dodge")) g2(iris, asp(x = Species, y = Sepal.Length, color = Species)) %>% fig_boxplot(adjust("dodge"))
Add a candle figure to the chart.
fig_candle( g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "range" )
fig_candle( g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "range" )
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
alias |
Name of the range to display on tooltips, labels, etc. |
Requires the following aspects defined:
open
close
high
low
If no color
argument is passed the candles are colored
according to their trend (open > close = "up").
stock <- structure( list( date = structure(c(18626, 18627, 18631, 18632), class = "Date"), open = c(39.52, 39.330002, 40.169998, 41.5), high = c( 39.73, 40, 41.560001, 42.040001 ), low = c( 39.200001, 39.029999, 39.939999, 40.77 ), close = c( 39.34, 39.880001, 41.400002, 41.16 ) ), row.names = c(NA, -4L), class = c( "tbl_df", "tbl", "data.frame" ) ) g2(stock, asp(date, open = open, close = close, high = high, low = low)) %>% fig_candle() %>% gauge_x_time_cat()
stock <- structure( list( date = structure(c(18626, 18627, 18631, 18632), class = "Date"), open = c(39.52, 39.330002, 40.169998, 41.5), high = c( 39.73, 40, 41.560001, 42.040001 ), low = c( 39.200001, 39.029999, 39.939999, 40.77 ), close = c( 39.34, 39.880001, 41.400002, 41.16 ) ), row.names = c(NA, -4L), class = c( "tbl_df", "tbl", "data.frame" ) ) g2(stock, asp(date, open = open, close = close, high = high, low = low)) %>% fig_candle() %>% gauge_x_time_cat()
Add a contour line figure to the chart.
fig_contour( g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE, colors = NULL, nlevels = 10, binwidth, levels, criticalRatio = 5, type = c("line", "filled") )
fig_contour( g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE, colors = NULL, nlevels = 10, binwidth, levels, criticalRatio = 5, type = c("line", "filled") )
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
colors |
A palette of colors to define the |
nlevels |
Passed to |
binwidth |
Passed to |
levels |
Passed to |
criticalRatio |
Passed to |
type |
Whether to draw the lines or polygons. |
Requires the x
, y
and z
aspects, the
width of the error bars can be changed with the size
aspect.
data(volcano) x <- 1:nrow(volcano) y <- 1:ncol(volcano) df <- expand.grid(x = x, y = y) df$z <- apply(df, 1, function(x) { volcano[x[1], x[2]] }) g <- g2(df, asp(x, y, z = z)) fig_contour(g) fig_contour(g, colors = c("red", "blue")) fig_contour(g, type = "filled", colors = c("darkblue", "white"))
data(volcano) x <- 1:nrow(volcano) y <- 1:ncol(volcano) df <- expand.grid(x = x, y = y) df$z <- apply(df, 1, function(x) { volcano[x[1], x[2]] }) g <- g2(df, asp(x, y, z = z)) fig_contour(g) fig_contour(g, colors = c("red", "blue")) fig_contour(g, type = "filled", colors = c("darkblue", "white"))
Add a density figure to the chart.
fig_density( g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "density" )
fig_density( g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "density" )
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
alias |
Name of the density curve. |
Requires the x
aspects.
g2(cars, asp(speed)) %>% fig_density() g2(iris, asp(Sepal.Width, color = Species)) %>% fig_density()
g2(cars, asp(speed)) %>% fig_density() g2(iris, asp(Sepal.Width, color = Species)) %>% fig_density()
Add an edge figure.
fig_edge(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_edge(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
Add an error bar figure to the chart.
fig_error( g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "error" )
fig_error( g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "error" )
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
alias |
Name of the range to display on tooltips, labels, etc. |
Requires the ymin
and ymax
aspects, the
width of the error bars can be changed with the size
aspect.
df <- data.frame( x = as.factor(c(1:10, 1:10)), y = runif(20, 10, 15), grp = rep(c("A", "B"), each = 2) ) df$ymin <- df$y - runif(20, 1, 2) df$ymax <- df$y + runif(20, 1, 2) g2(df, asp(x = x, color = grp)) %>% fig_error(asp(ymin = ymin, ymax = ymax), adjust("dodge")) %>% fig_interval( asp(y = y), adjust("dodge"), fillOpacity = .4 )
df <- data.frame( x = as.factor(c(1:10, 1:10)), y = runif(20, 10, 15), grp = rep(c("A", "B"), each = 2) ) df$ymin <- df$y - runif(20, 1, 2) df$ymax <- df$y + runif(20, 1, 2) g2(df, asp(x = x, color = grp)) %>% fig_error(asp(ymin = ymin, ymax = ymax), adjust("dodge")) %>% fig_interval( asp(y = y), adjust("dodge"), fillOpacity = .4 )
Add a path figure.
fig_heatmap(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_heatmap(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
Add a histogram figure to the chart.
fig_histogram( g, ..., bin_width = 5, sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "count" )
fig_histogram( g, ..., bin_width = 5, sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "count" )
g |
An object of class |
... |
Options to pass to the figure, including |
bin_width |
Width of bin. |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
alias |
Name of the range to display on tooltips, labels, etc. |
df <- data.frame( grp = rep(c("A", "B"), each = 200), val = c( rnorm(200, mean = 57, sd = 5), rnorm(200, mean = 53, sd = 5) ) ) g2(df, asp(val, color = grp)) %>% fig_histogram(adjust("stack"), bin_width = 1)
df <- data.frame( grp = rep(c("A", "B"), each = 200), val = c( rnorm(200, mean = 57, sd = 5), rnorm(200, mean = 53, sd = 5) ) ) g2(df, asp(val, color = grp)) %>% fig_histogram(adjust("stack"), bin_width = 1)
Add a line figure.
fig_line(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_line(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
g2(CO2, asp(conc, uptake, color = Plant)) %>% fig_line()
g2(CO2, asp(conc, uptake, color = Plant)) %>% fig_line()
Add a map figure.
fig_map(g, ..., inherit_asp = TRUE, sync = TRUE, map = get_world_map())
fig_map(g, ..., inherit_asp = TRUE, sync = TRUE, map = get_world_map())
g |
An object of class |
... |
Options to pass to the figure, including |
inherit_asp |
Whether to inherit the aspects paseed to
|
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
map |
Name of map to pass to the |
g2() %>% fig_map(stroke = "#fff", fill = "gray") %>% axis_hide()
g2() %>% fig_map(stroke = "#fff", fill = "gray") %>% axis_hide()
Add a path figure.
fig_path(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_path(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
df <- data.frame( x = runif(100), y = runif(100) ) g2(df, asp(x, y)) %>% fig_path()
df <- data.frame( x = runif(100), y = runif(100) ) g2(df, asp(x, y)) %>% fig_path()
Add a pie figure to the chart.
fig_pie(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_pie(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
df <- data.frame( label = letters[1:5], value = runif(5) ) g2(df, asp(y = value, color = label)) %>% fig_pie()
df <- data.frame( label = letters[1:5], value = runif(5) ) g2(df, asp(y = value, color = label)) %>% fig_pie()
Add a point figure.
fig_point(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_point(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
g2(cars) %>% fig_point(asp(speed, dist)) g2(mtcars, asp(mpg, disp, size = qsec)) %>% fig_point(asp(color = "red", shape = "square"))
g2(cars) %>% fig_point(asp(speed, dist)) g2(mtcars, asp(mpg, disp, size = qsec)) %>% fig_point(asp(color = "red", shape = "square"))
Add a polygon figure.
fig_polygon(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_polygon(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
Add a range figure to the chart.
fig_range( g, ..., type = c("interval", "area"), sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "range" )
fig_range( g, ..., type = c("interval", "area"), sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "range" )
g |
An object of class |
... |
Options to pass to the figure, including |
type |
Type of figure to use. |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
alias |
Name of the range to display on tooltips, labels, etc. |
Requires the ymin
and ymax
aspects.
df <- data.frame( x = 1:100, ymin = runif(100, 1, 5), ymax = runif(100, 6, 13) ) g2(df, asp(x, ymin = ymin, ymax = ymax)) %>% fig_range()
df <- data.frame( x = 1:100, ymin = runif(100, 1, 5), ymax = runif(100, 6, 13) ) g2(df, asp(x, ymin = ymin, ymax = ymax)) %>% fig_range()
Add a ribbon figure to the chart.
fig_ribbon( g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "ribbon" )
fig_ribbon( g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE, alias = "ribbon" )
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
alias |
Name of the range to display on tooltips, labels, etc. |
Requires the ymin
and ymax
aspects.
df <- data.frame( x = 1:100, ymin = runif(100, 1, 5), ymax = runif(100, 6, 13) ) g2(df, asp(x, ymin = ymin, ymax = ymax)) %>% fig_ribbon()
df <- data.frame( x = 1:100, ymin = runif(100, 1, 5), ymax = runif(100, 6, 13) ) g2(df, asp(x, ymin = ymin, ymax = ymax)) %>% fig_ribbon()
Add a rug figure to the chart.
fig_rug( g, ..., strokeOpacity = 0.5, axis = c("x", "y"), sync = TRUE, data = NULL, inherit_asp = TRUE )
fig_rug( g, ..., strokeOpacity = 0.5, axis = c("x", "y"), sync = TRUE, data = NULL, inherit_asp = TRUE )
g |
An object of class |
... |
Options to pass to the figure, including |
strokeOpacity |
Opacity of rug marks. |
axis |
Axis to place the rug marks on. |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
Requires the x
and y
aspects.
g2(mtcars, asp(wt, mpg)) %>% fig_point() %>% fig_rug() %>% fig_rug(asp(size = 10), axis = "y")
g2(mtcars, asp(wt, mpg)) %>% fig_point() %>% fig_rug() %>% fig_rug(asp(size = 10), axis = "y")
Add a schema figure.
fig_schema(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_schema(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
Add a segments figure to the chart.
fig_segment(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_segment(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
Add a smooth(ing) figure to the chart.
fig_smooth( g, ..., method = c("linear", "gaussian", "cosine", "epanechnikov", "quartic", "triangular", "tricube", "triweight", "uniform", "polynomial", "logarithmic", "boxcar", "power", "exponential"), band_width = 1, sync = TRUE, data = NULL, inherit_asp = TRUE )
fig_smooth( g, ..., method = c("linear", "gaussian", "cosine", "epanechnikov", "quartic", "triangular", "tricube", "triweight", "uniform", "polynomial", "logarithmic", "boxcar", "power", "exponential"), band_width = 1, sync = TRUE, data = NULL, inherit_asp = TRUE )
g |
An object of class |
... |
Options to pass to the figure, including |
method |
Smoothing method to use. |
band_width |
Step size for Silverman's algorithm. |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
Requires the x
and y
aspects.
This is a convenience function for a quick smoothing, see the online documentation to see how to use your own model for more control.
g2(cars, asp(speed, dist)) %>% fig_point() %>% fig_smooth(method = "gaussian") g2(iris, asp(Sepal.Width, Sepal.Length, color = Species)) %>% fig_point() %>% fig_smooth()
g2(cars, asp(speed, dist)) %>% fig_point() %>% fig_smooth(method = "gaussian") g2(iris, asp(Sepal.Width, Sepal.Length, color = Species)) %>% fig_point() %>% fig_smooth()
Add a voronoi figure to the chart.
fig_voronoi(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_voronoi(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
Requires the x
, y
, and color
arguments.
df <- data.frame( x = runif(25, 1, 500), y = runif(25, 1, 500), value = runif(25, 1, 500) ) g2(df, asp(x, y, color = value)) %>% fig_voronoi()
df <- data.frame( x = runif(25, 1, 500), y = runif(25, 1, 500), value = runif(25, 1, 500) ) g2(df, asp(x, y, color = value)) %>% fig_voronoi()
Add a waffle figure to the chart.
fig_waffle( g, ..., n = 500, rows = 10, size = c(1, 1), gap = 0.1, min_size = 15, sync = TRUE, data = NULL, inherit_asp = TRUE )
fig_waffle( g, ..., n = 500, rows = 10, size = c(1, 1), gap = 0.1, min_size = 15, sync = TRUE, data = NULL, inherit_asp = TRUE )
g |
An object of class |
... |
Options to pass to the figure, including |
n |
Number of squares to use. |
rows |
Number of rows. |
size |
Size of squares. |
gap |
Gap between squares. |
min_size |
Minimum size of squares. |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
Requires the x
and color
aspects.
fruits <- data.frame( fruit = c("Apples", "Bananas", "Pears", "Oranges"), value = c(.45, .15, .35, .05) * 100 ) g2(fruits, asp(value, color = fruit)) %>% fig_waffle() %>% motif(padding = 50) %>% axis_hide()
fruits <- data.frame( fruit = c("Apples", "Bananas", "Pears", "Oranges"), value = c(.45, .15, .35, .05) * 100 ) g2(fruits, asp(value, color = fruit)) %>% fig_waffle() %>% motif(padding = 50) %>% axis_hide()
Add an interval figure.
fig_interval(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
fig_interval(g, ..., sync = TRUE, data = NULL, inherit_asp = TRUE)
g |
An object of class |
... |
Options to pass to the figure, including |
sync |
Whether to sync the axis data (align) with that
used in other figures, set to |
data |
A dataset ( |
inherit_asp |
Whether to inherit the aspects paseed to
|
g2(sleep, asp(ID, extra, color = group)) %>% fig_interval() df <- data.frame( cat = letters[1:5], value = c(0.15, .3, .65, .75, .9) ) g2(df, asp(cat, value, color = cat, shape = "funnel")) %>% fig_interval(adjust("symmetric")) %>% coord_type("rect") %>% coord_transpose() %>% coord_scale(-1, 1) %>% axis_hide()
g2(sleep, asp(ID, extra, color = group)) %>% fig_interval() df <- data.frame( cat = letters[1:5], value = c(0.15, .3, .65, .75, .9) ) g2(df, asp(cat, value, color = cat, shape = "funnel")) %>% fig_interval(adjust("symmetric")) %>% coord_type("rect") %>% coord_transpose() %>% coord_scale(-1, 1) %>% axis_hide()
Initialise a chart.
g2( data = NULL, ..., width = NULL, height = NULL, elementId = NULL, digits = NULL, reorder = TRUE )
g2( data = NULL, ..., width = NULL, height = NULL, elementId = NULL, digits = NULL, reorder = TRUE )
data |
A data.frame or tibble containing data to chart,
an object of class |
... |
Aspects of the chart, see |
width , height
|
Dimensions of the chart, accepts
any valid CSS unit e.g.: |
elementId |
Valid CSS id attribute. |
digits |
Maximum number of digits after the comma to show on the chart. |
reorder |
Whether to internally reorder the data, namely
the |
g2(cars) %>% fig_point(asp(speed, dist))
g2(cars) %>% fig_point(asp(speed, dist))
Include dynamic elements in Rmarkdown.
g2_action(plot_id, btn_id, ..., data = NULL, reorder = TRUE)
g2_action(plot_id, btn_id, ..., data = NULL, reorder = TRUE)
plot_id |
Id of chart to interact with. |
btn_id |
Id of the |
... |
Aspects, see |
data |
Data.frame containing data to plot. |
reorder |
Whether to internally reorder the data, namely
the |
Proxy to dynamically interact with the chart in shiny.
g2_proxy(id, ..., data = NULL, session = shiny::getDefaultReactiveDomain())
g2_proxy(id, ..., data = NULL, session = shiny::getDefaultReactiveDomain())
id |
Id of chart to interact with. |
... |
Aspects, see |
data |
Data.frame containing data to plot. |
session |
A valid shiny session. |
library(shiny) dataset <- data.frame(x = 1:100, y = runif(100, 1, 100)) ui <- fluidPage( g2Output("plot"), actionButton("add", "Add figure") ) server <- function(input, output, session) { output$plot <- renderG2({ g2(dataset, asp(x, y)) %>% fig_point() }) observeEvent(input$add, { df <- data.frame(x = 1:100, y = runif(100, 1, 100)) g2_proxy("plot", data = df) %>% fig_point(asp(x, y)) %>% render() }) } if (interactive()) { shinyApp(ui, server) }
library(shiny) dataset <- data.frame(x = 1:100, y = runif(100, 1, 100)) ui <- fluidPage( g2Output("plot"), actionButton("add", "Add figure") ) server <- function(input, output, session) { output$plot <- renderG2({ g2(dataset, asp(x, y)) %>% fig_point() }) observeEvent(input$add, { df <- data.frame(x = 1:100, y = runif(100, 1, 100)) g2_proxy("plot", data = df) %>% fig_point(asp(x, y)) %>% render() }) } if (interactive()) { shinyApp(ui, server) }
Output and render functions for using g2r within Shiny applications and interactive Rmd documents.
g2Output(outputId, width = "100%", height = "400px") renderG2(expr, env = parent.frame(), quoted = FALSE)
g2Output(outputId, width = "100%", height = "400px") renderG2(expr, env = parent.frame(), quoted = FALSE)
outputId |
output variable to read from |
width , height
|
Must be a valid CSS unit (like |
expr |
An expression that generates a g2r |
env |
The environment in which to evaluate |
quoted |
Is |
Gauge the variables (aspects
) used to define axis
and grid of the plot.
gauge( g, asp, ..., nice = TRUE, range = NULL, min = NULL, max = NULL, min_limit = NULL, max_limit = NULL, alias = NULL, tick_count = NULL, max_tick_count = NULL ) gauge_x_time(g, ..., show_last = FALSE) gauge_y_time(g, ..., show_last = FALSE) gauge_x_linear(g, ..., tick_interval = NULL) gauge_y_linear(g, ..., tick_interval = NULL) gauge_x_cat(g, ...) gauge_y_cat(g, ...) gauge_x_time_cat(g, ...) gauge_y_time_cat(g, ...) gauge_x_log(g, ..., base = 10) gauge_y_log(g, ..., base = 10) gauge_x_pow(g, ...) gauge_y_pow(g, ...) gauge_x_quantile(g, ...) gauge_y_quantile(g, ...) gauge_x_quantize(g, ...) gauge_y_quantize(g, ...) gauge_x_identity(g, ...) gauge_y_identity(g, ...) gauge_asp(g, ...)
gauge( g, asp, ..., nice = TRUE, range = NULL, min = NULL, max = NULL, min_limit = NULL, max_limit = NULL, alias = NULL, tick_count = NULL, max_tick_count = NULL ) gauge_x_time(g, ..., show_last = FALSE) gauge_y_time(g, ..., show_last = FALSE) gauge_x_linear(g, ..., tick_interval = NULL) gauge_y_linear(g, ..., tick_interval = NULL) gauge_x_cat(g, ...) gauge_y_cat(g, ...) gauge_x_time_cat(g, ...) gauge_y_time_cat(g, ...) gauge_x_log(g, ..., base = 10) gauge_y_log(g, ..., base = 10) gauge_x_pow(g, ...) gauge_y_pow(g, ...) gauge_x_quantile(g, ...) gauge_y_quantile(g, ...) gauge_x_quantize(g, ...) gauge_y_quantize(g, ...) gauge_x_identity(g, ...) gauge_y_identity(g, ...) gauge_asp(g, ...)
g |
An object of class |
asp |
Bare column name of aspect to apply the gauge to. |
... |
Options to gauge variables
( |
nice |
Automatically adjust |
range |
A vector of length 2 giving the minimum, and maximum. |
min , max
|
Range of the gauge. |
min_limit , max_limit
|
Strict range of the ticks. |
alias |
Alias name of the gauge and variable to display. |
tick_count |
Maximum number of ticks. |
max_tick_count |
Maximum number of ticks. |
show_last |
Whether to force show the last tick
(only for |
tick_interval |
Minimum tick interval, only applies to linear type of gauge. |
base |
Base of log. |
cat
: Categorical.
timeCat
: Categorical time.
linear
: Linear.
time
: Date, time, etc.
log
: Logarithmic.
pow
: Exponential.
quantize
: Manual quantiles.
quantile
: Auto-generated quantiles.
identity
: Constant.
g <- g2(cars, asp(speed, dist)) %>% fig_point() g %>% gauge(speed, min = 0) g %>% gauge_y_log(title = "Log") g %>% gauge(dist, tickCount = 10)
g <- g2(cars, asp(speed, dist)) %>% fig_point() g %>% gauge(speed, min = 0) g %>% gauge_y_log(title = "Log") g %>% gauge(dist, tickCount = 10)
Customise aspects of the chart.
gauge_color(g, ...) gauge_size(g, ...) gauge_shape(g, ...) gauge_tooltip(g, ...) gauge_label(g, ...) gauge_style(g, ...) gauge_interplay(g, ...)
gauge_color(g, ...) gauge_size(g, ...) gauge_shape(g, ...) gauge_tooltip(g, ...) gauge_label(g, ...) gauge_style(g, ...) gauge_interplay(g, ...)
g |
An object of class |
... |
Arguments to customise the gauge.
Generally, key value pairs of options, a vector of hex colors,
or a JavaScript function (wrapped in |
gauge to gauge aspects of the grid and axis.
# base plot g <- g2(cars, asp(speed, dist)) %>% fig_point(asp(color = speed)) # color with vector g %>% gauge_color(c("red", "white", "blue")) # color with callback cb <- "function(speed){ if(speed > 10){ return 'blue'; } return 'red'; }" g %>% gauge_color(htmlwidgets::JS(cb))
# base plot g <- g2(cars, asp(speed, dist)) %>% fig_point(asp(color = speed)) # color with vector g %>% gauge_color(c("red", "white", "blue")) # color with callback cb <- "function(speed){ if(speed > 10){ return 'blue'; } return 'red'; }" g %>% gauge_color(htmlwidgets::JS(cb))
Maximum number of digits to show on charts.
global_digits(n = 16L)
global_digits(n = 16L)
n |
Maximum number of digits. |
Add a point figure.
info_text(g, ..., style = NULL, data = NULL) info_image(g, ..., style = NULL, data = NULL) info_arc(g, ..., style = NULL, data = NULL) info_line(g, ..., style = NULL, data = NULL) info_vline(g, ..., style = NULL, data = NULL) info_hline(g, ..., style = NULL, data = NULL) info_abline(g, ..., style = NULL, data = NULL, direction = c(1, 2)) info_region(g, ..., style = NULL, data = NULL) info_region_filter(g, ..., style = NULL, data = NULL) info_marker(g, ..., style = NULL, data = NULL) info_data_region(g, ..., style = NULL, data = NULL) info_shape(g, ..., style = NULL, data = NULL) info_html(g, ..., style = NULL, data = NULL)
info_text(g, ..., style = NULL, data = NULL) info_image(g, ..., style = NULL, data = NULL) info_arc(g, ..., style = NULL, data = NULL) info_line(g, ..., style = NULL, data = NULL) info_vline(g, ..., style = NULL, data = NULL) info_hline(g, ..., style = NULL, data = NULL) info_abline(g, ..., style = NULL, data = NULL, direction = c(1, 2)) info_region(g, ..., style = NULL, data = NULL) info_region_filter(g, ..., style = NULL, data = NULL) info_marker(g, ..., style = NULL, data = NULL) info_data_region(g, ..., style = NULL, data = NULL) info_shape(g, ..., style = NULL, data = NULL) info_html(g, ..., style = NULL, data = NULL)
g |
An object of class |
... |
Options to pass to the informational annotation. |
style |
A list of options defning the style. |
data |
A dataset to use with |
direction |
Direction of diagonal line. |
info_vline
, and info_hline
use the x
, and y
asp()
for placement.
Official annotation documentation
for defails pon what to pass to ...
, and asp()
.
df <- head(cars, 5) g2(cars, asp(speed, dist)) %>% fig_point() %>% info_text( position = c(20, 35), content = "Look here!" ) %>% info_text( asp(speed, dist), content = "Using aspects", data = df ) g2(cars, asp(speed, dist)) %>% fig_point() %>% info_vline(asp(x = 20)) %>% info_hline(asp(y = 20))
df <- head(cars, 5) g2(cars, asp(speed, dist)) %>% fig_point() %>% info_text( position = c(20, 35), content = "Look here!" ) %>% info_text( asp(speed, dist), content = "Using aspects", data = df ) g2(cars, asp(speed, dist)) %>% fig_point() %>% info_vline(asp(x = 20)) %>% info_hline(asp(y = 20))
Add a button input.
input_button(id, label, class = "default")
input_button(id, label, class = "default")
id |
Id of the button. |
label |
Label to display. |
class |
Class of the button. |
The class
argument defines the style of
the button in Bootstrap 3, generally accepts:for
default
info
success
warning
danger
Select Input
input_select(id, label, choices)
input_select(id, label, choices)
id |
Valid CSS id of the element. |
label |
Label to display. |
choices |
Vector of choices |
Add a slider to an R markdown document.
input_slider(id, label, value, min, max, step = 1)
input_slider(id, label, value, min, max, step = 1)
id |
Valid CSS id of the element. |
label |
Label to display. |
value |
Initial value of the slider. |
min , max
|
Minimum and maximum value the slider can be set. |
step |
Interval between steps. |
input_slider( "mySlider", "The label", value = 5, min = 0, max = 10 )
input_slider( "mySlider", "The label", value = 5, min = 0, max = 10 )
Configure global interplay (interactions) for the chart.
See gauge_interplay()
to customise figure-level interplay.
interplay(g, ...) remove_interplay(g, ...) register_interplay(g, name, ...)
interplay(g, ...) remove_interplay(g, ...) register_interplay(g, name, ...)
g |
An object of class |
... |
String(s) defining interactions. |
name |
Name of interaction to register |
# global interaction on chart df <- data.frame( x = letters, y = runif(26) ) g2(df, asp(x, y)) %>% fig_interval( selected(fill = "orange") ) %>% interplay("element", "selected") # brush g2(cars, asp(speed, dist)) %>% fig_point(asp(interplay = "brush")) # register df <- data.frame( x = c(letters, letters), y = runif(52), grp = c(rep("a", 26), rep("b", 26)) ) g2(df, asp(x, y, color = grp)) %>% fig_interval( asp(interplay = "element-highlight-by-color"), adjust("dodge") ) %>% register_interplay( "element-highlight-by-color", start = list( list( trigger = "element:mouseenter", action = "element-highlight-by-color:highlight" ) ), end = list( list( trigger = "element:mouseleave", action = "element-highlight-by-color:reset" ) ) )
# global interaction on chart df <- data.frame( x = letters, y = runif(26) ) g2(df, asp(x, y)) %>% fig_interval( selected(fill = "orange") ) %>% interplay("element", "selected") # brush g2(cars, asp(speed, dist)) %>% fig_point(asp(interplay = "brush")) # register df <- data.frame( x = c(letters, letters), y = runif(52), grp = c(rep("a", 26), rep("b", 26)) ) g2(df, asp(x, y, color = grp)) %>% fig_interval( asp(interplay = "element-highlight-by-color"), adjust("dodge") ) %>% register_interplay( "element-highlight-by-color", start = list( list( trigger = "element:mouseenter", action = "element-highlight-by-color:highlight" ) ), end = list( list( trigger = "element:mouseleave", action = "element-highlight-by-color:reset" ) ) )
Layout as arc using the alter package.
layout_arc( g, sourceWeight = NULL, targetWeight = NULL, thickness = 0.05, marginRatio = 0.1 )
layout_arc( g, sourceWeight = NULL, targetWeight = NULL, thickness = 0.05, marginRatio = 0.1 )
g |
An object of class |
sourceWeight , targetWeight
|
Bare name of column containing weights of source and target in the edges data.frame. |
thickness |
Node height, between |
marginRatio |
Space ratio, between |
ig <- igraph::erdos.renyi.game(100, 1 / 100) g2(ig, asp(x, y)) %>% layout_arc() %>% fig_edge(asp(color = source, shape = "arc"), opacity = .3) %>% fig_point(asp(color = id, shape = "circle", size = value)) %>% coord_type("polar") %>% coord_reflect("y") %>% axis_hide() g2(ig, asp(x, y)) %>% layout_arc() %>% fig_edge(asp(color = source, shape = "arc"), opacity = .3) %>% fig_point(asp(color = id, shape = "circle", size = value))
ig <- igraph::erdos.renyi.game(100, 1 / 100) g2(ig, asp(x, y)) %>% layout_arc() %>% fig_edge(asp(color = source, shape = "arc"), opacity = .3) %>% fig_point(asp(color = id, shape = "circle", size = value)) %>% coord_type("polar") %>% coord_reflect("y") %>% axis_hide() g2(ig, asp(x, y)) %>% layout_arc() %>% fig_edge(asp(color = source, shape = "arc"), opacity = .3) %>% fig_point(asp(color = id, shape = "circle", size = value))
Layout the graph using an igraph layout function.
This function only works with the graph was initialised
with an object of class igraph
.
layout_igraph(g, ..., method = igraph::layout_nicely)
layout_igraph(g, ..., method = igraph::layout_nicely)
g |
An object of class |
... |
Any options to pass to the |
method |
An igraph layout function to compute the nodes and edges (source and target) position on the canvas. |
The function runs the method
to obtain the x
and y
coordinates. These are added to the nodes
data.frame (extracted from initial graph) and to the edges
data.frame, as x
and y
nested columns, e.g.:
c(source_x, target_x)
. These x
and y
coordinates
can be used in asp()
(see example).
ig <- igraph::make_ring(100) # use x and y for positioning g2(ig, asp(x, y)) %>% layout_igraph() %>% fig_edge() %>% fig_point(asp(shape = "circle")) %>% axis_hide()
ig <- igraph::make_ring(100) # use x and y for positioning g2(ig, asp(x, y)) %>% layout_igraph() %>% fig_edge() %>% fig_point(asp(shape = "circle")) %>% axis_hide()
Retrieve map data to pass to the maps
argument of the fig_map()
function.
get_gadm_data(iso3c, level = c(0, 1, 2, 3, 4), keep = 0.05) get_map_data(region = ".", level = c("region", "subregion"), name = "world") get_world_map()
get_gadm_data(iso3c, level = c(0, 1, 2, 3, 4), keep = 0.05) get_map_data(region = ".", level = c("region", "subregion"), name = "world") get_world_map()
iso3c |
Iso3c code of the country to retrieve, e.g.: |
level |
Level of the polygons to draw, either the region or subregion. |
keep |
Proportion of points to retain, it is highly recommended
to reduce the detail of the map or it will take too long to load in the
browser. Set to |
region |
Character vector that names the polygons to draw. |
name |
Name of the database to use. |
get_gadm_data
: Retrieves country-level data from gadm.org.
get_map_data
: Uses the maps::map()
function to retrieve the map data, similar to ggplot2::map_data
.
get_world_map
: Retrives a world map (from GeoJSON).
Set the motif of the chart, defaults to light
.
motif( g, ..., brandColor = NULL, backgroundColor = "transparent", renderer = c("canvas", "svg"), padding = "auto", visible = TRUE ) global_motif( ..., brandColor = NULL, backgroundColor = "transparent", renderer = c("canvas", "svg"), padding = "auto", visible = TRUE ) motif_from_json(g, path) motif_from_list(g, motif) motif_as_list(..., brandColor = NULL, backgroundColor = "transparent")
motif( g, ..., brandColor = NULL, backgroundColor = "transparent", renderer = c("canvas", "svg"), padding = "auto", visible = TRUE ) global_motif( ..., brandColor = NULL, backgroundColor = "transparent", renderer = c("canvas", "svg"), padding = "auto", visible = TRUE ) motif_from_json(g, path) motif_from_list(g, motif) motif_as_list(..., brandColor = NULL, backgroundColor = "transparent")
g |
An object of class |
... |
Key value pair defining style, or
|
brandColor |
Main default color. |
backgroundColor |
Plot background color. |
renderer |
Renderer to use, defaults to |
padding |
An integer, or a vector of length 4. |
visible |
Whether the chart is visible. |
path |
Path to JSON file. |
motif |
List defnining the theme, similar to JSON. |
The function motif_from_json()
can be used to
define the theme from a JSON file of theme, to see the
default theme file:
system.file("theme.json", package = "g2r")
.
motif
: Defines the motif of a visualisation.
motif_from_json
: Defines the motif from a JSON
file of theme, see the theme file.
system.file("theme.json", package = "g2r")
motif_from_list
: Defines the motif from a list
,
derived from the JSON file.
motif_as_list
: Returns a motif as a list
to use
with motif_from_list()
.
global_motif
: Define a global motif that will be used by
all subsequent charts.
g2(iris, asp(Sepal.Width, Sepal.Length)) %>% fig_point( asp(color = Species, shape = "circle") ) %>% motif( brandColor = "orange", backgroundColor = "black", elementPoint( shape = "circle", stroke = "white", fillOpacity = .7 ) )
g2(iris, asp(Sepal.Width, Sepal.Length)) %>% fig_point( asp(color = Species, shape = "circle") ) %>% motif( brandColor = "orange", backgroundColor = "black", elementPoint( shape = "circle", stroke = "white", fillOpacity = .7 ) )
Convenience function to create a new animation,
equivalent to Animation$new()
.
new_animation()
new_animation()
Convenience function to easily apply colors palettes.
gauge_color_viridis(g) gauge_color_plasma(g) gauge_color_inferno(g) gauge_color_magma(g) gauge_color_accent(g) gauge_color_dark2(g) gauge_color_paired(g) gauge_color_pastel1(g) gauge_color_pastel2(g) gauge_color_set1(g) gauge_color_set2(g) gauge_color_set3(g) gauge_color_neon(g) gauge_color_std(g) gauge_color_pink(g) gauge_color_orange(g) gauge_color_lime(g) gauge_color_blue(g) gauge_color_aw(g) gauge_color_g2rq(g) gauge_color_g2rc(g) gauge_color_g2rd(g) gauge_color_brbg(g) gauge_color_piyg(g) gauge_color_prgn(g) gauge_color_puor(g) gauge_color_rdbu(g) gauge_color_rdgy(g) gauge_color_rdylbu(g) gauge_color_rdylgn(g) gauge_color_spectral(g) gauge_color_flashy(g) gauge_color_red(g) gauge_color_ryb(g) gauge_color_bwg(g)
gauge_color_viridis(g) gauge_color_plasma(g) gauge_color_inferno(g) gauge_color_magma(g) gauge_color_accent(g) gauge_color_dark2(g) gauge_color_paired(g) gauge_color_pastel1(g) gauge_color_pastel2(g) gauge_color_set1(g) gauge_color_set2(g) gauge_color_set3(g) gauge_color_neon(g) gauge_color_std(g) gauge_color_pink(g) gauge_color_orange(g) gauge_color_lime(g) gauge_color_blue(g) gauge_color_aw(g) gauge_color_g2rq(g) gauge_color_g2rc(g) gauge_color_g2rd(g) gauge_color_brbg(g) gauge_color_piyg(g) gauge_color_prgn(g) gauge_color_puor(g) gauge_color_rdbu(g) gauge_color_rdgy(g) gauge_color_rdylbu(g) gauge_color_rdylgn(g) gauge_color_spectral(g) gauge_color_flashy(g) gauge_color_red(g) gauge_color_ryb(g) gauge_color_bwg(g)
g |
An object of class |
Palletes from the viridisLite
package, ideal for:
continuous data.
gauge_color_viridis
(continuous)
gauge_color_plasma
(continuous)
gauge_color_inferno
(continuous)
gauge_color_magma
(continuous)
Palettes from color brewer:
gauge_color_accent
(qualitative)
gauge_color_dark2
(qualitative)
gauge_color_paired
(qualitative)
gauge_color_pastel1
(qualitative)
gauge_color_pastel2
(qualitative)
gauge_color_set1
(qualitative)
gauge_color_set2
(qualitative)
gauge_color_set3
(qualitative)
gauge_color_brbg
(diverging)
gauge_color_piyg
(diverging)
gauge_color_prgn
(diverging)
gauge_color_puor
(diverging)
gauge_color_rdbu
(diverging)
gauge_color_rdgy
(diverging)
gauge_color_rdylbu
(diverging)
gauge_color_rdylgn
(diverging)
gauge_color_spectral
(diverging)
Palettes taken from coolors.co:
gauge_color_neon
(continuous)
gauge_color_std
(continuous)
gauge_color_orange
(continuous)
gauge_color_pink
(continuous)
gauge_color_lime
(continuous)
gauge_color_blue
(continuous)
gauge_color_red
(discrete)
gauge_color_flashy
(discrete)
gauge_color_ryb
(discrete)
gauge_color_bwg
(diverging)
Palettes from awtools
package:
gauge_color_aw
(qualitative)
Custom:
gauge_color_g2rc
(continuous)
gauge_color_g2rq
(qualitative)
gauge_color_g2rd
(diverging)
Split the chart into planes according to variables.
planes( g, asp, ..., type = c("rect", "list", "matrix", "circle", "tree", "mirror"), sync = TRUE )
planes( g, asp, ..., type = c("rect", "list", "matrix", "circle", "tree", "mirror"), sync = TRUE )
g |
An object of class |
asp |
Aspects that define split, these must be defined as a formula, e.g.: ~x+y. |
... |
Any other option. |
type |
Type of planes to use. |
sync |
Whether to sync the aspects used for the planes
with others used elsewhere, similar to that of |
g2(iris, asp(Sepal.Length, Sepal.Width, color = Species)) %>% fig_point() %>% planes(~Species, type = "tree")
g2(iris, asp(Sepal.Length, Sepal.Width, color = Species)) %>% fig_point() %>% planes(~Species, type = "tree")
Draw a quick plot.
qg2(object, ..., conf_level = 0.95, intercept = FALSE, names = NULL)
qg2(object, ..., conf_level = 0.95, intercept = FALSE, names = NULL)
object |
An object containing data to plot, often a model. |
... |
Ignored |
conf_level |
Confidence level. |
intercept |
Whether to display the intercept. |
names |
Names of the models. |
Add a checkbox or radio input.
input_checkbox(id, label, choices, selected = NULL, inline = TRUE) input_radio(id, label, choices, selected = NULL, inline = TRUE)
input_checkbox(id, label, choices, selected = NULL, inline = TRUE) input_radio(id, label, choices, selected = NULL, inline = TRUE)
id |
Id of input. |
label |
Label of the input. |
choices |
Vector of choices to define either the checboxes or radio inputs. |
selected |
Vector of |
inline |
Whether the input is inline. |
Remove a figure from the plot.
remove_figure(g, index)
remove_figure(g, index)
g |
An object of class |
index |
Index of figure to remove. Either
the numeric index of figure (layer number in order
it was added to the visualisation), or the |
g <- g2(mtcars, asp(qsec, wt)) %>% fig_point(config(id = "myPoints")) %>% fig_point(asp(y = drat)) # all figures g # remove figure remove_figure(g, "myPoints") library(shiny) df <- data.frame( x = 1:100, y = runif(100), z = runif(100) ) ui <- fluidPage( g2Output("plot"), actionButton("rm", "Randomly remove a figure") ) server <- function(input, output) { output$plot <- renderG2({ g2(df, asp(x, y)) %>% fig_point() %>% fig_line(asp(y = z)) }) observeEvent(input$rm, { g2_proxy("plot") %>% remove_figure(sample(1:2, 1)) }) } if (interactive()) { shinyApp(ui, server) }
g <- g2(mtcars, asp(qsec, wt)) %>% fig_point(config(id = "myPoints")) %>% fig_point(asp(y = drat)) # all figures g # remove figure remove_figure(g, "myPoints") library(shiny) df <- data.frame( x = 1:100, y = runif(100), z = runif(100) ) ui <- fluidPage( g2Output("plot"), actionButton("rm", "Randomly remove a figure") ) server <- function(input, output) { output$plot <- renderG2({ g2(df, asp(x, y)) %>% fig_point() %>% fig_line(asp(y = z)) }) observeEvent(input$rm, { g2_proxy("plot") %>% remove_figure(sample(1:2, 1)) }) } if (interactive()) { shinyApp(ui, server) }
Render proxy calls.
render(g, update = TRUE)
render(g, update = TRUE)
g |
An object of class |
update |
Whether to trigger the update process. |
Add a scrollbar to the chart.
scrollbar(g, ...)
scrollbar(g, ...)
g |
An object of class |
... |
Options to pass to the slider. |
The official documentation
for the list options to pass to ...
.
Add a slider to the chart.
slider(g, ...)
slider(g, ...)
g |
An object of class |
... |
Options to pass to the slider. |
The official documentation
for the list options to pass to ...
.
Customise the styles of figures given states (active or selected).
active(...) selected(...)
active(...) selected(...)
... |
Key value pair passed to styles. |
g2(iris, asp(Sepal.Width, Sepal.Length)) %>% fig_point( selected(fill = "red") ) %>% interplay("element", "selected")
g2(iris, asp(Sepal.Width, Sepal.Length)) %>% fig_point( selected(fill = "red") ) %>% interplay("element", "selected")
Add a subject to the plot.
subject(g, subject, tag = htmltools::h3)
subject(g, subject, tag = htmltools::h3)
g |
An object of class |
subject |
Subject of chart to display. |
tag |
|
g2(cars, asp(speed, dist)) %>% fig_point() %>% subject("Points")
g2(cars, asp(speed, dist)) %>% fig_point() %>% subject("Points")
Convenience function to create tooltip templates
(itemTp
argument in tooltip()
function).
tpl(...) tpl_item(name, value, marker = TRUE)
tpl(...) tpl_item(name, value, marker = TRUE)
... |
One or more |
name , value
|
Name and value of the tooltip item. |
marker |
Whether to include the color marker (dot) in the tooltip. |
The arguments title
, name
, and value
accept
either a bare column name from the data to use as
{mustache}
/{handlebar}
in the template. If a string is
passed then it is treated as constant.
template <- tpl( tpl_item( island, bill_depth_mm ) )
template <- tpl( tpl_item( island, bill_depth_mm ) )
Converts objects to objects g2r can work with,
generally a tibble::tibble
.
to_g2r(data = NULL)
to_g2r(data = NULL)
data |
An object to convert. |
This is exposed so the user can understand
what happens under the hood and which variables/columns
can subsequently be used in figures with asp()
.
These methods are used in the g2()
function to preprocess
data
objects.
## Not run: to_g2r(AirPassengers) ## End(Not run)
## Not run: to_g2r(AirPassengers) ## End(Not run)
Configure the tooltip applied to the entire chart.
See gauge_tooltip()
to customise a specific tooltip
(the tooltip of a specific figure).
tooltip(g, ...)
tooltip(g, ...)
g |
An object of class |
... |
Options to pass to the axis, pass |
g2(mtcars, asp(drat, qsec, color = hp)) %>% fig_point() %>% tooltip( showCrosshairs = TRUE, crosshairs = list(type = "xy") )
g2(mtcars, asp(drat, qsec, color = hp)) %>% fig_point() %>% tooltip( showCrosshairs = TRUE, crosshairs = list(type = "xy") )