• Stars
    star
    177
  • Rank 209,457 (Top 5 %)
  • Language
    R
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

3d extension to ggplot

ggrgl

Lifecycle: experimental

ggrgl extends ggplot2 into the third dimension.

ggrgl does this by adding a new z aesthetic which is respected when a plot is rendered to the {devoutrgl} device which renders to OpenGL via {rgl}.

Because {devoutrgl} is an interactive device, the view of the plot may be manipulated by the user - zoom, rotate, change field-of-view etc.

Slideshow

Note: interactive 3d plots cannot be displayed in a github README file, and static images or animated gifs are used here instead.

Visit the pkgdown online documentation to view plots which can be manipulated in the browser.

Installation

A lot of support packages are needed for ggrgl - most of which should already be installed if you have ggplot.

Custom packages which are needed (and not currently on CRAN):

  • devout - The core package for writing graphics devices using R (rather than C).
  • devoutrgl - a graphics device which renders to {rgl}
  • triangular - decompaose polygons into triangles for 3d rendering. Much faster than rgl::triangulate()
  • snowcrash - a package for encoding objects as images - needed to circumvent limitations in the device system provided by R
  • cryogenic - a package for capturing a call to be evaluated later. Calls to generate 3d geometry are generated when plot object is created, but not executed until later when the plot is actually rendered to screen.

You can install from GitHub with:

# install.package('remotes')
remotes::install_github('coolbutuseless/devout')
remotes::install_github('coolbutuseless/devoutrgl')
remotes::install_github('coolbutuseless/triangular')
remotes::install_github('coolbutuseless/snowcrash')
remotes::install_github('coolbutuseless/cryogenic')
remotes::install_github('coolbutuseless/ggrgl', ref='main')

New z Aesthetic

The new z aesthetic works the same as the x and y aesthetics in ggplot2.

z may be mapped to a data variable or set to a constant.

New Extrusion Aesthetics

When graphical elements have a z value they are raised above the ground.

By setting extrude = TRUE (on geoms which support it), then the raised element is connected to the ground as if it were extruded from it.

New aesthetics control the appearance of the extruded faces and edges:

aesthetic Description default
extrude_z Lower limit of extrusion 0.05
extrude_face_fill Extruded face colour grey20
extrude_face_alpha Extruded face alpha 1
extrude_edge_colour Edge colour for extrusion NA
extrude_edge_alpha Edge alpha for extrusion 1
extrude_edge_size Width of line for extruded edges 1

3-dimensional Geometry Types: z and 3d

ggrgl defines 2 classes of geoms: z and 3d

  • z geoms are identical to their ggplot2 counterparts, except the entire shape may be raised (as a unit) in the z direction. The resulting geometry will still be planar, and parallel to the original plotting surface.
  • 3d geoms allow for full specification of (x, y, z) locations. Their orientation is not constrained to be planar, or parallel to the original plotting surface.

Click on a geom in the following table to view its vignette

ggplot2 Planar Z offset 3d
geom_bar geom_bar_z
geom_contour geom_contour_z
geom_contour_filled geom_contour_filled_z
geom_density geom_density_z
geom_line geom_line_3d
geom_path geom_path_3d
geom_point geom_point_z geom_sphere_3d
geom_polygon geom_polygon_z
geom_rect geom_rect_z
geom_ribbon geom_ribbon_z
geom_segment geom_segment_3d
geom_text geom_text_z
geom_tile geom_tile_z

Vignettes

There are vignettes on all the new geoms in this package, see links in the table above or click on an image below

Some vignettes with some details of the implementation and usage are:

  • Technical Overview gives some details on how the 3d geometry is communicated internally
  • Z Scaling shows examples how the scaling of the Z coordinate may be controlled.
  • Keep 2d β€˜shadows’ demonstrates how the 2d shadow of 3d geometry may be kept for some supported geoms.

To see all vignettes see the online documentation

Dark Side of the Moon DNA Pie
Joy Division Volcano Terrain
Bunny Caffeine Molecule USA Map

Example - A Basic 3d Bar Plot with geom_bar_z()

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Always explicitly load required packages - there are some possible 
# issues if you don't!
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
suppressPackageStartupMessages({
  library(rgl)
  library(devout)
  library(devoutrgl)
  library(ggrgl)
  library(ggplot2)
})

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Use `geom_bar_z` and set `z` to 200 and use extrusion
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
p <- ggplot(mpg) +
  geom_bar_z(aes(x=class, fill=class), colour='black', z=200, extrude=TRUE)


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Render Plot in 3d with {devoutrgl}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
devoutrgl::rgldev()
p
invisible(dev.off())

Related Software

  • ggplot2 - a grammar of graphics implementation for 2d plots in R. ggrgl would be impossible without ggplot2 to build upon.

Acknowledgements

  • Hadley Wickham, Thomas Lin Pedersen and others for developing and maintaining ggplot2
  • Michael Sumner + Tyler Morgan-Wall on twitter for their in-depth technical advice on graphics and 3d in R
  • R Core for developing and maintaining the language.
  • CRAN maintainers, for patiently shepherding packages onto CRAN and maintaining the repository

More Repositories

1

dplyr-cli

Manipulate CSV files on the command line using dplyr
R
267
star
2

ggsvg

Use SVG images as ggplot points
R
134
star
3

tickle

Easily create UIs in base R
R
122
star
4

yyjsonr

Fast JSON package for R
C
102
star
5

emphatic

ANSI colouring of R output
R
100
star
6

devout

Write R graphics output devices in plain R
C++
97
star
7

anotherworld

AnotherWorld ported to R
R
69
star
8

ggreverse

Reverse a ggplot object back into code
R
67
star
9

eventloop

Event Loop in R
R
63
star
10

isocubes

R
62
star
11

wordle

Wordle helper for RStats
R
59
star
12

devoutsvg

Bespoke SVG graphics output device with pattern fill support
R
57
star
13

rllama

Minimal R wrapper for llama.cpp
C
53
star
14

ggthreed

3d geoms and stats for ggplot
R
46
star
15

svgparser

Render SVG images in R. Load SVG data as data.frames
R
44
star
16

threed

Three-Dimensional Object Transformations
R
43
star
17

nara

nativeRaster tools for R
C
43
star
18

minipdf

Minimal pure-R PDF document creator
R
41
star
19

gluestick

Simple, single-function string interpolation in Base R
R
41
star
20

carelesswhisper

Automatic speech recognition in R using whisper.cpp
C
40
star
21

tr808r

TR-808 Drum Machine for R
R
39
star
22

xxhashlite

Very fast hash functions using xxHash
C++
35
star
23

geomlime

ggplot geom_lime()
R
34
star
24

facetious

Home to some alternate facetting for ggplot2
R
34
star
25

ggblur

Blurry Point Geom for ggplot2
R
34
star
26

fugly

Extract named substrings using named capture groups in regular expressions.
R
33
star
27

minicss

Build CSS selectors, styles and stylesheets within R
R
31
star
28

minisvg

Create SVG documents with R
R
29
star
29

rbytecode

R bytecode assembler/disassembler
R
26
star
30

numberwang

numbers to words and vice versa
R
25
star
31

zstdlite

Fast, configurable in-memory compression of R objects with zstd
C
25
star
32

pacman

Pacman-ish game in R
R
24
star
33

memoisetools

a collection of additional caches and helper functions to work alongside the memoise package
R
23
star
34

wingspan

Data about the birds and bonus cards in the boardgame "Wingspan"
R
23
star
35

terse

Terse output for nested lists and data.frames
R
22
star
36

ransid

Convert images to ANSI with R
R
21
star
37

lz4lite

Very Fast compression/decompression of in-memory numeric vectors with LZ4
C
20
star
38

miranda

Fast PRNGs for R
C
20
star
39

hershey

Hershey vector font data for RStats
R
20
star
40

simplercpp

A demo R package incorporating C code with Rcpp
C++
20
star
41

chipmunkcore

R wrapper around the Chipmunk2d physics simulation library
C
19
star
42

minihtml

A package for building HTML documents in R (shiny compatible)
R
18
star
43

foist

Fast Output of Images
C++
17
star
44

simplecall

A demo R package incorporating C code which is called with .Call()
C
17
star
45

displease

Non-linear numeric interpolation
R
16
star
46

CP1919

Cambridge Pulsar at 19 hours and 19 minutes right ascension
R
16
star
47

purler

Fast run-length encoding with NA support and results as a data.frame
C
16
star
48

RStudioConf-2022

16
star
49

CRAN-checks

Notes about extra CRAN checks
16
star
50

nonogram

Nonogram solver in rstats
R
15
star
51

minidrawio

Create simple draw.io documents from R
R
15
star
52

optout

Optimized Graphics Output
R
15
star
53

ggqr

ggplot2 geom for QR codes
R
15
star
54

gridfont

A version of the 'gridfont' vector font in an R friendly format
R
15
star
55

phon

Tools and Data for the CMU Pronouncing Dictionary
R
14
star
56

rd2list

Convert Rd documentation to a structured, human-readable list
R
14
star
57

flagon

Flags of the World
R
14
star
58

chipmunkbasic

Higher-level R6 wrapper around Chipmunk2d rigid body physics simulation library
R
14
star
59

anon

Anonymous function creation in R and purrr
R
14
star
60

miniverse

A constellation of packages for document creation in R
R
14
star
61

devoutaudio

An R graphics device which renders to audio
R
13
star
62

image2xlsx

Convert an image to an excel spreadsheet
R
13
star
63

insitu

In-place modification of vectors
C
13
star
64

strictlyr

Stricter subset of dplyr
R
13
star
65

svgpatternusgs

SVG patterns from the U.S. Geological Survey for use within R
R
13
star
66

cstructr

Exposing C structrs to R
C
13
star
67

ingrid

Tools for interactive grid creation and manipulation in the console
R
13
star
68

devoutdrawio

R graphics device to output to draw.io XML vector format
R
13
star
69

ggecho

ggplot2 stat for echoing data
R
13
star
70

arcadefont

Oldschool arcade vector font
R
13
star
71

cgrep

Highlighted grep of R objects
R
12
star
72

serializer

Example showing how to access R's serialization functions from C
C
12
star
73

devoutrgl

R graphics device to render to {rgl}
R
12
star
74

getrect

Partition matrix into single-valued rectangular areas
R
12
star
75

visvalingam

R package for Visvalingam Line Simplification
C
11
star
76

smallfactor

An R factor backed by a raw vector rather than an integer vector.
R
11
star
77

analemmatic

Create analemmatic sundials with R
R
11
star
78

fastpng

Read/write 8-bit/16-bit PNGs with rasters, native rasters, numeric+integer arrays, indexed images with palette, packed pixels in raw vector. Configurable compression settings allow for speed/size tradeoff.
C
11
star
79

cairocore

Low-level bindings to the CairoGraphics library for fast 2d drawing operations
R
11
star
80

bdftools

Bitmap font tools for R
R
11
star
81

simplefortran

Demo of how Fortran code could be included in an R package
C
10
star
82

svgpatternsimple

Create some simple repeating SVG patterns in R
R
10
star
83

snowcrash

Encode arbitrary objects as PNGs, rasters and rasterGrobs
R
9
star
84

callme

Compile and load C code for use with .Call()
R
9
star
85

frak

Fractal Generator
C
9
star
86

grrr

Modify function default arguments
R
9
star
87

triangular

Decompose complex polygons into sets of triangles
R
9
star
88

cryogenic

Freezing calls, modifying arguments and evaluating later
R
8
star
89

devoutpdf

A hand-crafted PDF graphics output device written in plain R
R
8
star
90

btnvips

libvips cli wrapper for #RStats (btn = better than nothing)
R
8
star
91

ggdebug

Package to help debug and inspect ggplot stats
R
8
star
92

simplec

Demo R package with C code which is called using ".C()'
C
8
star
93

poissoned

Poisson Disc Sampling in R
R
8
star
94

moggy

R
8
star
95

flexo

Simple lex/parse tools in R
R
7
star
96

cssparser

Parse CSS into R and apply to HTML
R
7
star
97

cairobasic

A basic R6 wrapper around cairocore
R
7
star
98

svgfilter

Create example SVG filters in R
R
7
star
99

sqlitemeta

Parse SQLite CREATE TABLE statements into R objects
C
7
star
100

lofi

Low-fidelity, low-bit representations of integers, doubles and colours.
R
7
star