knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

library(g2r)

g2r comes with a number of figures that can be used to create visualisations. However, these can be used in many different ways than what is presented here in order to produce completely different looking charts; see examples, and methods.

See the graph vignette for network-related figures.

Line

The line can be controlled with shape:

df <- data.frame(
  x = 1:99,
  y = rnorm(99, mean = rep(c(65,70,75),each=33), sd = 2) 
)

g2(df, asp(x, y)) %>% 
  fig_line()

Step

A step chart is a line chart with a shape, one of: vh, hv, hvh, or vhv.

g2(df, asp(x, y)) %>% 
  fig_line(asp(shape = "hv"))

Point

The shape of the point can also be set to:

g2(iris, asp(Sepal.Width, Sepal.Length, color = Species)) %>% 
  fig_point()

Area

The shape of the area can also be set to:

blockchain <- jsonlite::fromJSON(
  "https://gw.alipayobjects.com/os/antvdemo/assets/data/blockchain.json"
)

g2(blockchain, asp(date, blockchain)) %>% 
  fig_area()

Path

df <- data.frame(
  x = runif(50),
  y = runif(50) 
)

g2(df, asp(x, y)) %>% 
  fig_path()

Interval

The shape of the interval can also be set to:

df <- data.frame(
  x = seq.Date(Sys.Date() - 29, Sys.Date(), by = "days"),
  y = rnorm(30, mean = rep(c(70,60),each=15), sd = 3)
)

g2(df, asp(x, y)) %>% 
  fig_interval()

Polygon

df <- expand.grid(x = letters, y = letters)
df$value <- rnorm(nrow(df))

g2(df, asp(x, y, color = value)) %>% 
  fig_polygon() %>% 
  gauge_color_blue()

Heatmap

g2(iris, asp(Sepal.Length, Sepal.Width, color = Petal.Length)) %>% 
  fig_heatmap() %>% 
  gauge_color(c("blue", "cyan", "lime", "yellow", "red"))

Bin

Use count (internally computed) to define the color (optional).

g2(cars, asp(speed, dist, color = count)) %>% 
  fig_bin() %>% 
  gauge_color_blue()

Use size_count to size the bins by count.

data(diamonds, package = "ggplot2")

g2(diamonds, asp(table, price, color = count)) %>% 
  fig_bin(
    type = "hex", 
    size_count = FALSE,
    stroke = "#fff"
  ) %>% 
  gauge_color_blue()

Ribbon

df <- data.frame(
 x = 1:50,
 y = rnorm(50, mean = rep(c(67,73),each=25), sd = 2)
)

df$ymin <- df$y - runif(50, 2, 3)
df$ymax <- df$y + runif(50, 2, 4)

g2(df, asp(x, ymin = ymin, ymax = ymax)) %>% 
  fig_line(asp(y = y)) %>% 
  fig_ribbon()

Histogram

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(bin_width = 1, fillOpacity = .5)

Density

g2(iris, asp(Sepal.Length, color = Species)) %>% 
  fig_density()

Boxplot

df <- tidyr::pivot_longer(iris, -Species)

g2(df, asp(name, value, color = Species)) %>% 
 fig_boxplot(adjust("dodge"))

Smooth

g2(cars, asp(speed, dist)) %>% 
  fig_point() %>% 
  fig_smooth(asp(shape = "smooth"), method = "polynomial")

Range

Two types: interval and area.

df <- data.frame(
 x = 1:10,
 ymin = runif(10, 1, 5),
 ymax = runif(10, 6, 13)
)

g2(df, asp(x, ymin = ymin, ymax = ymax)) %>% 
 fig_range()
g2(df, asp(x, ymin = ymin, ymax = ymax)) %>% 
 fig_range(type = "area")

Pie

df <- data.frame(
 name = letters[1:5],
 value = runif(5)
)

g2(df, asp(y = value, color = name, label = name)) %>% 
 fig_pie()

Voronoi

df <- dplyr::tibble(
  x = runif(50, 1, 500),
  y = runif(50, 1, 500),
  value = runif(50, 1, 500)
)

g2(df, asp(x, y, color = value)) %>% 
  fig_voronoi() %>% 
  gauge_x_linear(nice = FALSE) %>% 
  gauge_y_linear(nice = FALSE) %>% 
  gauge_color_blue()

Waffle

fruits <- dplyr::tibble(
  fruit = c("Apples", "Bananas", "Pears", "Oranges"),
  value = c(.45, .15, .35, .05) * 100
)

g2(fruits, asp(value, color = fruit)) %>% 
  fig_waffle() %>% 
  motif(padding = c(10, 10, 30, 10)) %>% 
  axis_hide()

Rug

g2(mtcars, asp(wt, mpg)) %>% 
  fig_point() %>% 
  fig_smooth(asp(shape = "smooth"), method = "polynomial") %>% 
  fig_rug(asp(size = 10)) %>% 
  fig_rug(asp(size = 10), axis = "y")

Candle

wallgreens  <- tidyquant::tq_get("WBA", from = Sys.Date() - 90)

cb <- htmlwidgets::JS(
  "(trend) => {
    if(trend == 'Up')
      return 'green';

    return 'red';
  }"
)

g2(wallgreens, asp(date, open = open, close = close, high = high, low = low)) %>% 
  fig_candle() %>% 
  gauge_x_time_cat() %>% 
  gauge_color(cb)

Error bars

df <- data.frame(
  x = as.factor(c(1:10, 1:10)),
  y = runif(20, 15, 25),
  grp = rep(c("A", "B"), each = 2)
)

df$ymin <- df$y - runif(20, 1, 3)
df$ymax <- df$y + runif(20, 1, 3)

g2(df, asp(x = x, color = grp)) %>% 
  fig_error(
    asp(ymin = ymin, ymax = ymax, size = 10), 
    adjust("dodge")
  ) %>% 
  fig_interval(
    asp(y = y), 
    adjust("dodge"),
    fillOpacity = .4
  ) 

Contour

data(faithfuld, package = "ggplot2")

g2(faithfuld, asp(waiting, eruptions, z = density)) %>% 
  fig_contour(
    binwidth = 0.001,
    colors = c("#440154FF", "#21908CFF", "#FDE725FF")
  )
g2(faithfuld, asp(eruptions, waiting, z = density)) %>% 
  fig_contour(
    type = "filled", 
    colors = c("#000004FF", "#B63679FF", "#FCFDBFFF")
  ) %>% 
  gauge_x_linear(nice = FALSE) %>% 
  gauge_y_linear(nice = FALSE)

Segments

seg <- df <- data.frame(
  x = c(24, 23),
  y = c(70, 54),
  xend = c(17, 24),
  yend = c(50, 93)
)

g2(cars, asp(speed, dist)) %>% 
  fig_point() %>% 
  fig_segment(
    asp(x = x, y = y, xend = xend, yend = yend),
    data = seg
  )

Explore quick plots



devOpifex/g2r documentation built on Jan. 16, 2022, 12:36 a.m.