Synthax

The synthax of g2r is heavily inspired by ggplot2.

g2r syntax
From ggplot2 to g2r
ggplot2 g2r
ggplot g2
aes asp
scale_* gauge_*
geom_* fig_*
facet_* plane_*
theme_* motif_*

Aspects

In g2r you use aspects (asp) instead of aesthetics (aes) in ggplot2 but they work in very much the same way.

g2(cars, asp(dist, speed, color = speed)) %>% 
  fig_point()

Then you can use gauges (instead of scales in ggplot2) to manipulate those aspects.

g2(cars, asp(dist, speed, color = speed, size = speed)) %>% 
  fig_point() %>% 
  gauge_color(c("blue", "white", "red")) 

It may seem like some some figures (equivalent to geoms) are missing, but technically all are available.

fruits %>% 
  arrange(-value) %>% 
  g2(asp(value, color = fruit)) %>% 
  fig_interval_stack() %>% 
  coord_type("theta")

The same could be said of the funnel.

fr <- fruits %>% 
  dplyr::mutate(value = value * 100) %>% 
  dplyr::arrange(-value)

g2(fr, asp(fruit, value, color = fruit, shape = "pyramid")) %>% 
  fig_interval_symmetric() %>% 
  coord_transpose() %>% 
  coord_scale(1, -1) %>% 
  hide_axes()

Examples

You won’t find fig_bar but fig_interval does the trick. Below we use the adjust function which is similar to using the stat argument in ggplot2.

g2(temp, asp(month, temp, color = city)) %>% 
  fig_interval(adjust("dodge")) # equivalent to fig_interval_dodge
g2(temp, asp(month, temp, color = city)) %>% 
  fig_interval(adjust("stack")) # equivalent to fig_interval_stack
library(dplyr)

df <- mtcars %>%
  dplyr::mutate(
    cyl = as.factor(cyl),
    am = as.factor(am)
  )

g2(df, asp(cyl , mpg, color = am)) %>%
  fig_boxplot()

There is a fig_violin but it expects the data in a specific format, so a helper function exists, fig_guitar.

df <- mtcars %>%
  dplyr::mutate(
    cyl = as.factor(cyl),
    am = as.factor(am)
  )

g2(df, asp(cyl , mpg, color = am)) %>%
  fig_guitar(asp(opacity = .3), tooltip = FALSE)

A heatmap is easier.

library(dplyr)

data("diamonds", package = "ggplot2")

palette <- c("blue", "cyan", "lime", "yellow", "red")

diamonds %>% 
  count(carat, price) %>% 
  g2(asp(carat, price, color = n)) %>% 
  fig_heatmap() %>% 
  gauge_color(palette)