knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(dplyr) library(g2r) library(gt) g2r_globals(font = "Quicksand")
The synthax of g2r is heavily inspired by ggplot2.
dplyr::tribble( ~"ggplot2", ~"g2r", "ggplot", "g2", "aes", "asp", "scale_*", "gauge_*", "geom_*", "fig_*", "facet_*", "plane_*", "theme_*", "motif_*" ) %>% gt() %>% tab_header( title = "g2r syntax", subtitle = "From ggplot2 to g2r" )
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()
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)
iris %>% g2(asp(Petal.Length, Petal.Width, color = Species)) %>% fig_point() %>% plane_wrap(planes(Species), type = "tree")
fruits %>% dplyr::mutate(value = value * 100) %>% g2(asp(fruit, value)) %>% fig_waffle(asp(color = fruit))
The alter
function is used internally by get_map
to process GeoJSON files. It'll enable you to easily build otherwise difficult charts. Though there is a fig_hex
helper now.
g2(gaus, asp(x, y, color = "count")) %>% fig_bin() %>% gauge_color(c("#BAE7FF", "#1890FF", "#0050B3"))
g2(gaus, asp(x, y, color = "count")) %>% fig_bin(size_count = FALSE, type = "hexagon") %>% gauge_color(c("#BAE7FF", "#1890FF", "#0050B3"))
df <- dplyr::tibble( x = runif(25, 1, 500), y = runif(25, 1, 500), value = runif(25, 1, 500) ) g2(df, asp(x, y, color = value)) %>% fig_voronoi(axes = FALSE)
fruits %>% arrange(value) %>% mutate(label = fruit) %>% g2(asp(fruit, value)) %>% fig_interval(asp(shape = "line")) %>% fig_point(asp(shape = "circle")) %>% coord_type("theta", innerRadius = .2, endAngle = pi) %>% info_text(asp(fruit, 0, content = label), style = list(textAlign = "right"))
Many figures have been implemented, many of them actually use the group
aspect.
g2(iris, asp(Sepal.Length, group = Species, color = Species)) %>% fig_histogram(bin_width = .3)
g2(iris, asp(Petal.Length, group = Species, color = Species)) %>% fig_density()
df <- data.frame( trt = factor(c(1, 1, 2, 2)), resp = c(1, 5, 3, 4), group = factor(c(1, 2, 1, 2)), upper = c(1.1, 5.3, 3.3, 4.2), lower = c(0.8, 4.6, 2.4, 3.6) ) g2(df, asp(trt, resp, ymin = lower, ymax = upper, color = group)) %>% fig_errorbar()
huron <- dplyr::tibble( year = as.character(1875:1972), level = as.vector(LakeHuron) ) %>% dplyr::mutate( mn = level - 1, mx = level + 1 ) g2(huron, asp(year, level, ymin = mn, ymax = mx)) %>% fig_line() %>% fig_ribbon() %>% gauge_x_time(tickCount = 3)
g2(cars, asp(speed, dist)) %>% fig_point() %>% fig_smooth()
In g2r you use motif
instead of theme
, though you can build your own with custom_motif
there is a build-in motif_dark
in addition to the default.
cars2 <- dplyr::mutate(cars, speed = jitter(speed)) g2(cars2, asp(speed, dist)) %>% fig_point() %>% fig_smooth(method = "loess") %>% fig_rug() %>% motif_dark()
There is a helper function to create animations (You might have to refresh the page to see it).
anim <- Animation$ new()$ appear(duration = 5000, delay = 5000) iris %>% g2(asp(Sepal.Length, Sepal.Width, color = Species)) %>% fig_point(anim)
Using "informational elements" to a chart. See documentation for the full list. Note the chart below is inspired by the Bretton Woods Wikipedia page which actually uses the same dataset from CRAN.
data("bankingCrises", package = "Ecdat") bw <- bankingCrises %>% tidyr::gather("country", "value", -year) %>% dplyr::group_by(year) %>% dplyr::summarise(n = sum(value)) g2(bw, asp(year, n)) %>% fig_interval() %>% info_region( start = c("1945", "min"), end = c("1971", "max"), inherit_asp = FALSE ) %>% info_text( content = "Bretton Woods", position = c("1945", 20), inherit_asp = FALSE )
You can pass lists as axis data.
library(g2r) library(purrr) df <- dplyr::tibble( x = letters[1:10], y1 = runif(10, 1, 5), y2 = runif(10, 7, 10) ) %>% tidyr::nest(y1, y2) df$data <- df$data %>% map(unlist) %>% map(unname) g2(df, asp(x, data)) %>% fig_point(asp(shape = "circle", size = 5)) %>% fig_interval(asp(size = 3))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.