gglite for ggplot2 Users

If you are familiar with ggplot2, you already know the Grammar of Graphics. gglite follows the same grammar but renders interactive JavaScript charts powered by AntV G2 instead of static R graphics. This vignette maps ggplot2 concepts to their gglite equivalents so you can get started quickly.

1 Key differences at a glance

ggplot2 gglite Notes
ggplot(data, aes(x, y)) g2(data, x = 'x', y = 'y') Column names are character strings, not bare symbols
+ operator |> or + gglite supports both
geom_point() mark_point() “geom” → “mark”
scale_x_log10() scale_x(type = 'log') Helper functions for each channel
coord_flip() coord_transpose()
coord_polar() coord_polar()
facet_wrap() / facet_grid() facet_rect() / facet_circle()
theme_minimal() theme_light()
labs(title = ...) titles(...)
Static PNG/PDF Interactive HTML/JS Charts support tooltips, brushing, filtering

2 No non-standard evaluation

The biggest syntax difference: in ggplot2 you write bare column names inside aes(); in gglite you always pass quoted strings:

# ggplot2
ggplot(mtcars, aes(mpg, hp, color = factor(cyl)))

# gglite
g2(mtcars, x = 'mpg', y = 'hp', color = 'cyl')

This means gglite works naturally with programmatic column names—no need for .data[[]] or !!sym().

3 Building blocks: geom → mark

Many ggplot2 geom_*() functions have corresponding mark_*() equivalents in gglite. Here are some common mappings:

ggplot2 gglite
geom_point() mark_point()
geom_line() mark_line()
geom_bar() / geom_col() mark_interval()
geom_area() mark_area()
geom_boxplot() mark_boxplot()
geom_text() mark_text()
geom_path() mark_path()
geom_polygon() mark_polygon()
geom_hline() mark_line_y()
geom_vline() mark_line_x()
geom_rect() mark_rect()
geom_tile() mark_cell()

gglite also provides marks without direct ggplot2 equivalents, such as mark_sankey(), mark_chord(), mark_word_cloud(), mark_treemap(), mark_liquid(), and mark_gauge().

4 Scatter plot

# ggplot2
ggplot(iris, aes(Sepal.Width, Sepal.Length,
                 color = Species)) +
  geom_point()
# gglite
g2(iris, x = 'Sepal.Width', y = 'Sepal.Length',
   color = 'Species') |>
  mark_point()

5 Bar chart

# ggplot2
df = data.frame(x = c('A', 'B', 'C', 'D'),
                y = c(3, 7, 2, 5))
ggplot(df, aes(x, y)) +
  geom_col()
# gglite
df = data.frame(x = c('A', 'B', 'C', 'D'),
                y = c(3, 7, 2, 5))
g2(df, x = 'x', y = 'y') |>
  mark_interval()

6 Line chart with groups

# ggplot2
df = data.frame(
  x = rep(1:5, 2),
  y = c(3, 1, 4, 1, 5, 2, 7, 1, 8, 3),
  g = rep(c('A', 'B'), each = 5)
)
ggplot(df, aes(x, y, color = g)) +
  geom_line()
# gglite
df = data.frame(
  x = rep(1:5, 2),
  y = c(3, 1, 4, 1, 5, 2, 7, 1, 8, 3),
  g = rep(c('A', 'B'), each = 5)
)
g2(df, x = 'x', y = 'y', color = 'g') |>
  mark_line()

7 Stacked and dodged bars

In ggplot2 you set position = "stack" or position = "dodge" inside the geom. In gglite, stacking and dodging are data transforms:

# ggplot2
ggplot(df, aes(x, y, fill = color)) +
  geom_col(position = "stack")
# gglite
df = data.frame(
  x = rep(c('A', 'B', 'C'), each = 2),
  y = c(3, 2, 5, 4, 1, 6),
  color = rep(c('a', 'b'), 3)
)
g2(df, x = 'x', y = 'y', color = 'color') |>
  mark_interval() |>
  transform('stackY')

For grouped (dodged) bars, replace transform('stackY') with transform('dodgeX').

8 Scales

In ggplot2 each scale has its own function (scale_x_log10(), scale_color_brewer(), etc.). In gglite there are helpers like scale_x(), scale_y(), and scale_color():

# ggplot2
ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  scale_y_log10()
# gglite
g2(mtcars, x = 'mpg', y = 'hp') |>
  scale_y(type = 'log')

Custom color palettes:

# gglite
g2(iris, x = 'Sepal.Width', y = 'Sepal.Length',
   color = 'Species') |>
  scale_color(range = c('#e41a1c', '#377eb8', '#4daf4a'))

9 Coordinates

# ggplot2
ggplot(df, aes(x, y)) +
  geom_col() +
  coord_flip()
# gglite
df = data.frame(x = c('A', 'B', 'C', 'D'),
                y = c(3, 7, 2, 5))
g2(df, x = 'x', y = 'y') |>
  mark_interval() |>
  coord_transpose()

Polar coordinates create rose or pie charts:

# gglite
g2(df, x = 'x', y = 'y', color = 'x') |>
  mark_interval() |>
  transform('stackY') |>
  coord_theta(innerRadius = 0.5)

10 Faceting

ggplot2’s facet_wrap() and facet_grid() correspond to facet_rect():

# ggplot2
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point() +
  facet_wrap(~Species)
# gglite
g2(iris, x = 'Sepal.Width', y = 'Sepal.Length') |>
  facet_rect(x = 'Species')

11 Themes

ggplot2 uses theme_*() functions; gglite also provides theme_*() shortcuts:

ggplot2 gglite
theme_gray() (default) theme_classic() (default)
theme_bw() / theme_minimal() theme_light()
theme_dark() theme_dark()
theme_classic_dark()
theme_academy()
# gglite
g2(mtcars, x = 'mpg', y = 'hp') |>
  theme_academy()

12 Titles and labels

In ggplot2 you use labs(); in gglite use titles():

# ggplot2
ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  labs(title = "Motor Trend Cars",
       subtitle = "mpg vs horsepower")
# gglite
g2(mtcars, x = 'mpg', y = 'hp') |>
  titles('Motor Trend Cars',
    subtitle = 'mpg vs horsepower')

13 What gglite adds

Because gglite renders to HTML/JavaScript, it offers features that ggplot2 cannot provide out of the box:

13.1 Built-in interactivity

Add tooltips, brushing, and filtering with interact():

# gglite
g2(iris, x = 'Sepal.Width', y = 'Sepal.Length',
   color = 'Species') |>
  interact('tooltip') |>
  interact('legendFilter')

Available interactions include tooltip, elementHighlight, brushHighlight, legendFilter, legendHighlight, and more.

13.2 Animations

Charts can animate on enter, update, and exit with animate(). This is especially useful for presentations and dashboards.

13.3 Lightweight output

gglite generates self-contained HTML with no R runtime dependency. The output works in any modern browser, making it easy to share via email, embed in websites, or include in R Markdown / litedown documents.

14 Using + instead of |>

If you are used to ggplot2’s + operator, you can use it in gglite too. Both operators produce identical results, and you can even mix them freely in the same expression:

# ggplot2 style (+ operator)
g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') +
  scale_color(palette = 'set2') +
  titles('Iris Dataset')

# Pipe style (|> operator)
g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |>
  scale_color(palette = 'set2') |>
  titles('Iris Dataset')

# Mixed (both work fine together)
g2(iris, x = 'Sepal.Width', y = 'Sepal.Length', color = 'Species') |>
  scale_color(palette = 'set2') + titles('Iris Dataset')

15 Quick-reference cheat sheet

# Start a chart
g2(data, x = 'col1', y = 'col2', color = 'col3')

# Add marks (geometries)
|> mark_point()
|> mark_line()
|> mark_interval()
|> mark_area()

# Transforms (position adjustments)
|> transform('stackY')
|> transform('dodgeX')

# Scales
|> scale_x(type = 'log')
|> scale_color(palette = 'viridis')

# Coordinates
|> coord_polar()
|> coord_theta()
|> coord_transpose()

# Facets
|> facet_rect(x = 'var')

# Theme
|> theme_dark()

# Components
|> titles('Title', subtitle = 'Subtitle')
|> tooltip(crosshairs = TRUE)
|> labels(text = 'y')

# Interactions
|> interact('tooltip')
|> interact('brushHighlight')