knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) h <- 250 library(g2r) library(dplyr) library(htmltools) n <- 9 df1 <- tibble::tibble( x = c(letters[1:n], letters[1:n]), y = c(rnorm(n, 10, 20), rnorm(n, 12, 19)), grp = rep(c("A", "B"), each = n) ) df2 <- tibble::tibble( x = rep(1:n, 2), y = c(rnorm(n, 10, 15), rnorm(n, 12, 17)), grp = rep(c("A", "B"), each = n) ) df3 <- data.frame( x = runif(20), y = rnorm(20, 20) ) df4 <- expand.grid(x = letters[1:(n*2)], y = letters[1:(n*2)]) df4$value <- rnorm(nrow(df4)) df5 <- data.frame( x = 1:10, y = rnorm(10, mean = 20, sd = 2) ) df5$ymin <- df5$y - runif(10, 1, 2) df5$ymax <- df5$y + runif(10, 1, 4) df6 <- data.frame( grp = rep(c("A", "B"), each = 200), val = c( rnorm(200, mean = 56, sd = 3), rnorm(200, mean = 53, sd = 5) ) ) df7 <- data.frame( x = 1:n, ymin = runif(n, 1, 5), ymax = runif(n, 6, 13) ) df8 <- data.frame( name = letters[1:n], value = runif(n) ) df9 <- dplyr::tibble( x = runif(n * 3, 1, 500), y = runif(n * 3, 1, 500), value = runif(n * 3, 1, 500) )
point <- g2(cars, asp(speed, dist), height = h) %>% fig_point() %>% tooltip( showCrosshairs = TRUE, crosshairs = list( type = "xy" ) ) interval <- g2(df1, asp(x, y, color = grp), height = h) %>% fig_interval(adjust("stack")) %>% tooltip(marker = FALSE) line <- g2(df2, asp(x, y, color = grp), height = h) %>% fig_line() %>% tooltip( showCrosshairs = TRUE ) div( class = "row", div( class = "col-sm-4", point ), div( class = "col-sm-4", interval ), div( class = "col-sm-4", line ) )
area <- df2 %>% mutate(y = runif(n * 2)) %>% g2(asp(x, y, color = grp), height = h) %>% fig_area(adjust("stack"), fillOpacity = 1) %>% tooltip(shared = TRUE) path <- g2(df3, asp(x, y), height = h) %>% fig_point(asp(shape = "square")) %>% fig_path() step <- df2 %>% mutate(y = runif(n * 2)) %>% g2(asp(x, y, color = grp), height = h) %>% fig_line(asp(shape = "vh")) %>% tooltip(shared = TRUE) div( class = "row", div( class = "col-sm-4", area ), div( class = "col-sm-4", path ), div( class = "col-sm-4", step ) )
polygon <- g2(df4, asp(x, y, color = value), height = h) %>% fig_polygon() %>% gauge_color(c("#BAE7FF", "#1890FF", "#0050B3")) heatmap <- g2(iris, asp(Sepal.Length, Sepal.Width, color = Petal.Length), height = h) %>% fig_heatmap() %>% gauge_color(c("blue", "cyan", "lime", "yellow", "red")) bin_square <- g2(cars, asp(speed, dist, color = count), height = h) %>% fig_bin() %>% gauge_color(c("#BAE7FF", "#1890FF", "#0050B3")) div( class = "row", div( class = "col-sm-4", polygon ), div( class = "col-sm-4", heatmap ), div( class = "col-sm-4", bin_square ) )
ribbon <- g2(df5, asp(x, ymin = ymin, ymax = ymax), height = h) %>% fig_line(asp(y = y)) %>% fig_ribbon() %>% info_vline(asp(x = 3, content = "Line")) %>% tooltip(showCrosshairs = TRUE, shared = TRUE) c <- cars c$speed <- c$speed + runif(1) data(diamonds, package = "ggplot2") bin_hex <- g2(diamonds, asp(carat, price, color = count), height = h) %>% fig_bin(type = "hexagon", size_count = FALSE, bins = c(20, 20)) %>% gauge_color(c("#BAE7FF", "#1890FF", "#0050B3")) histo <- g2(df6, asp(val, color = grp), height = h) %>% fig_histogram(bin_width = 1, fillOpacity = .5) %>% tooltip(shared = TRUE) div( class = "row", div( class = "col-sm-4", ribbon ), div( class = "col-sm-4", bin_hex ), div( class = "col-sm-4", histo ) )
dens <- g2(iris, asp(Sepal.Width, color = Species), height = h) %>% fig_density() iris_long <- tidyr::pivot_longer(iris, -Species) box <- iris_long %>% dplyr::filter(name %in% c("Petal.Length", "Sepal.Length")) %>% g2(asp(name, value, color = Species), height = h) %>% fig_boxplot(adjust("dodge")) smooth <- g2(mtcars, asp(qsec, mpg), height = h) %>% fig_point(asp(shape = "plus")) %>% fig_smooth(asp(shape = "smooth")) div( class = "row", div( class = "col-sm-4", box ), div( class = "col-sm-4", smooth ), div( class = "col-sm-4", dens ) )
rng_bar <- g2(df7, asp(x, ymin = ymin, ymax = ymax), height = h) %>% fig_range() %>% tooltip(marked = FALSE) rng_area <- g2(df7, asp(x, ymin = ymin, ymax = ymax), height = h) %>% fig_range(asp(shape = "smooth"), type = "area") %>% info_hline(asp(y = "median", content = "Median")) %>% tooltip( showCrosshairs = TRUE ) pie <- g2(df8, asp(y = value, color = name, label = name), height = h) %>% fig_pie() %>% tooltip(marker = FALSE) div( class = "row", div( class = "col-sm-4", rng_bar ), div( class = "col-sm-4", rng_area ), div( class = "col-sm-4", pie ) )
voronoi <- g2(df9, asp(x, y, color = value), height = h) %>% fig_voronoi() %>% gauge_x_linear(nice = FALSE) %>% gauge_y_linear(nice = FALSE) %>% gauge_color(c("#BAE7FF", "#1890FF", "#0050B3")) %>% tooltip(marker = FALSE) fruits <- dplyr::tibble( fruit = c("Apples", "Bananas", "Pears", "Oranges"), value = c(.45, .15, .35, .05) * 100 ) waffle <- g2(fruits, asp(value, color = fruit), height = h) %>% fig_waffle(n = 200, rows = 10, stroke = "white") %>% motif(padding = c(10, 10, 30, 10)) %>% axis_hide() rug <- g2(mtcars, asp(wt, mpg), height = h) %>% fig_point(asp(shape = "circle")) %>% fig_rug(asp(size = 4)) %>% fig_rug(asp(size = 4), axis = "y") div( class = "row", div( class = "col-sm-4", voronoi ), div( class = "col-sm-4", waffle ), div( class = "col-sm-4", rug ) )
wallgreens <- tidyquant::tq_get("WBA", from = Sys.Date() - 10) k <- g2(wallgreens, asp(date, open = open, close = close, high = high, low = low), height = h) %>% fig_candle() %>% gauge_x_time_cat() err <- df1 %>% mutate( ymin = y - runif(n, 1, 5), ymax = y + runif(n, 1, 2) ) %>% g2(asp(x = x, color = grp), height = h) %>% fig_error( asp(ymin = ymin, ymax = ymax, size = 10), adjust("dodge") ) %>% fig_interval( asp(y = y), adjust("dodge"), fillOpacity = .4 ) library(survival) fit <- survfit(Surv(time, status) ~ trt, data = survival::veteran) q <- qg2(fit) %>% tooltip(showCrosshairs = TRUE) q$height <- h div( class = "row", div( class = "col-sm-4", k ), div( class = "col-sm-4", err ), div( class = "col-sm-4", q ) )
cc <- acf(lh, plot = FALSE) acf <- qg2(cc) acf$height <- h library(yardstick) data(two_class_example) roc <- roc_curve(two_class_example, truth, Class1) r <- qg2(roc) r$height <- h library(forecast) fc <- forecast(ets(USAccDeaths)) f <- qg2(fc) %>% info_marker( position = c("1976-07-01", 10080), text = list( content = "Mark" ) ) f$height <- h div( class = "row", div( class = "col-sm-4", acf ), div( class = "col-sm-4", f ), div( class = "col-sm-4", r ) )
data(mpg, package = "ggplot2") l <- loess(hwy ~ displ, data = mpg, span = .3) %>% g2(asp(displ), height = h) %>% fig_point(asp(y = hwy, shape = "circle", size = 3), fillOpacity = .3, stroke = 0) %>% fig_line(asp(y = .fitted, shape = "smooth")) %>% fig_ribbon(asp(ymin = .lower, ymax = .upper, shape = "smooth")) json <- jsonlite::fromJSON( "https://gw.alipayobjects.com/os/antvdemo/assets/data/baby-names.json" ) steam <- json %>% dplyr::filter( year > 1940 ) %>% dplyr::group_by(name, year) %>% dplyr::summarise(n = sum(n)) %>% g2(asp(year, n, color = name, shape = "smooth"), height = h) %>% fig_area( adjust("stack"), adjust("symmetric"), fillOpacity = .85 ) %>% gauge_x_linear(tickInterval = 20) %>% legend_color(position = "right") spiral <- data.frame( x = 1:(n*15), y = rnorm(n*15, mean = 32, 5) ) %>% g2(asp(x, y, color = y), height = h) %>% fig_interval(asp(size = .2)) %>% coord_type("helix") %>% gauge_color(c("#ffffff", "#1890FF")) div( class = "row", div( class = "col-sm-4", l ), div( class = "col-sm-4", steam ), div( class = "col-sm-4", spiral ) )
library(forecast) fc <- forecast(ets(USAccDeaths)) %>% to_g2r() area_shade <- fc %>% dplyr::filter(!is.na(y)) %>% g2(asp(x, y), height = h) %>% fig_area(fill = "l(0) 0:#abd6f5 0.5:#7ec2f3 1:#0083fc") %>% fig_line() df10 <- data.frame( x = c(letters[1:7], letters[1:7]), y = round(runif(14, 20, 70)), grp = rep(c("A", "Z"), each = 7) ) polar <- g2(df10, asp(x, y, color = grp), height = h) %>% fig_interval(adjust("dodge", margin = 1)) %>% coord_type("polar") %>% interplay("active", "region") %>% tooltip( marker = FALSE, shared = TRUE ) fruits <- data.frame( x = c( "Apple", "Banana", "Orange", "Coconut", "Melon", "Grapes", "Strawberry", "Pear", "Kiwi", "Apricot" ), y = rev(c(1, 3, 4, 6, 9, 10, 12, 13, 14, 16)) ) rad <- g2(fruits, asp(x, y, color = y), height = h, reorder = FALSE) %>% fig_interval( asp(label = y), lineWidth = 1, stroke = "#fff" ) %>% coord_type( "polar", startAngle = pi, endAngle = pi * (3 / 2) ) %>% gauge_color(c("rgb(255,215,135)", "rgb(252,143,72)")) %>% gauge_y_linear(tickCount = 10) %>% interplay("element", "highlight") %>% axis_x( tickLine = list(aligntick = FALSE), grid = list(alignTick = FALSE) ) %>% axis_y( grid = list(closed = FALSE) ) %>% tooltip(marker = FALSE) %>% legend_color(FALSE) div( class = "row", div( class = "col-sm-4", area_shade ), div( class = "col-sm-4", polar ), div( class = "col-sm-4", rad ) )
traffic <- data.frame( label = c("Visit", "Portal", "Signup", "Pay", "Retain"), users = c(1, .7, .5, .3, .1) ) fun <- g2(traffic, asp(label, users, color = label), height = h, reorder = FALSE) %>% fig_interval( asp(shape = "funnel"), adjust("symmetric") ) %>% gauge_color(c('#0050B3', '#1890FF', '#40A9FF', '#69C0FF', '#BAE7FF')) %>% coord_transpose() %>% coord_scale(1, -1) %>% axis_hide() %>% tooltip(marker = FALSE) 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]] }) contour_filled <- g2(df, asp(x, y, z = z), height = h) %>% fig_contour( type = "filled", colors = c("#000004FF", "#BB3754FF", "#FCFFA4FF") ) df11 <- data.frame( grp = rep(c("FR", "US", "SA"), each = 50), values = rnorm( 150, rep(c(47, 37, 35), each = 50), rep(c(1, 2, 3), each = 50) ) ) polar_polar_box <- g2(df11, asp(grp, values, color = grp), height = h) %>% fig_boxplot(asp(size = 60)) %>% coord_type("polar", innerRadius = .2) %>% legend_color(position = "top") %>% gauge_y_linear(nice = FALSE) div( class = "row", div( class = "col-sm-4", fun ), div( class = "col-sm-4", contour_filled ), div( class = "col-sm-4", polar_polar_box ) )
radar <- g2(df1, asp(x, y, color = grp), height = h) %>% fig_area() %>% fig_line(asp(size = 2)) %>% fig_point(asp(size = 4, shape = "circle")) %>% coord_type("polar") g <- igraph::erdos.renyi.game(50, 2/50) graph <- qg2(g) graph$height <- h correl_mat <- cor(mtcars[,1:5]) mat <- qg2(correl_mat) %>% gauge_color(c("#c77dff", "#7b2cbf", "#240046")) mat$height <- h div( class = "row", div( class = "col-sm-4", radar ), div( class = "col-sm-4", graph ), div( class = "col-sm-4", mat ) )
data(faithfuld, package = "ggplot2") contour <- g2(faithfuld, asp(waiting, eruptions, z = density), height = h) %>% fig_contour(colors = c("#440154FF", "#21908CFF", "#FDE725FF")) weekday <- c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun") employee <- LETTERS[1:10] roster <- expand.grid(weekday = weekday, employee = employee) roster$sales <- round(runif(nrow(roster), 10, 50)) radian <- g2(roster, asp(employee, weekday, color = sales), height = h) %>% fig_polygon( lineWidth = 1, stroke = "#fff" ) %>% gauge_color(c("#BAE7FF", "#1890FF", "#0050B3")) %>% coord_type("polar", innerRadius = .2) %>% axis_y( grid = NULL, line = NULL, tickLine = NULL, label = NULL ) %>% axis_x( grid = NULL, line = NULL, tickLine = NULL ) seg <- df <- data.frame( x = c(4, 24, 20), y = c(2, 70, 52), xend = c(8, 25, 23), yend = c(16, 85, 54) ) seg <- g2(cars, asp(speed, dist), height = h) %>% fig_point() %>% fig_segment( asp(x = x, y = y, xend = xend, yend = yend), data = seg ) div( class = "row", div( class = "col-sm-4", contour ), div( class = "col-sm-4", radian ), div( class = "col-sm-4", seg ) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.