• Stars
    star
    182
  • Rank 211,154 (Top 5 %)
  • Language
    R
  • License
    MIT License
  • Created almost 4 years ago
  • Updated almost 4 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
269
star
2

ggsvg

Use SVG images as ggplot points
R
138
star
3

emphatic

Highlighting R output in the console
R
137
star
4

yyjsonr

Fast JSON package for R
C
124
star
5

tickle

Easily create UIs in base R
R
123
star
6

devout

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

anotherworld

AnotherWorld ported to R
R
75
star
8

ggreverse

Reverse a ggplot object back into code
R
67
star
9

eventloop

Event Loop in R
R
65
star
10

isocubes

R
61
star
11

nara

nativeRaster tools for R
C
60
star
12

wordle

Wordle helper for RStats
R
59
star
13

devoutsvg

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

rllama

Minimal R wrapper for llama.cpp
C
55
star
15

carelesswhisper

Automatic speech recognition in R using whisper.cpp
C
54
star
16

svgparser

Render SVG images in R. Load SVG data as data.frames
R
48
star
17

ggthreed

3d geoms and stats for ggplot
R
46
star
18

threed

Three-Dimensional Object Transformations
R
43
star
19

minipdf

Minimal pure-R PDF document creator
R
41
star
20

gluestick

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

tr808r

TR-808 Drum Machine for R
R
40
star
22

xxhashlite

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

facetious

Home to some alternate facetting for ggplot2
R
36
star
24

geomlime

ggplot geom_lime()
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
34
star
27

rbytecode

R bytecode assembler/disassembler
R
34
star
28

minicss

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

minisvg

Create SVG documents with R
R
29
star
30

zstdlite

Fast, configurable in-memory compression of R objects with zstd
C
26
star
31

numberwang

numbers to words and vice versa
R
25
star
32

pacman

Pacman-ish game in R
R
24
star
33

wingspan

Data about the birds and bonus cards in the boardgame "Wingspan"
R
24
star
34

memoisetools

a collection of additional caches and helper functions to work alongside the memoise package
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

chipmunkcore

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

simplercpp

A demo R package incorporating C code with Rcpp
C++
19
star
42

minihtml

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

simplecall

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

callme

Easily compile inline C code for R
R
17
star
45

foist

Fast Output of Images
C++
17
star
46

RStudioConf-2022

17
star
47

minidrawio

Create simple draw.io documents from R
R
16
star
48

displease

Non-linear numeric interpolation
R
16
star
49

CP1919

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

purler

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

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
16
star
52

ggqr

ggplot2 geom for QR codes
R
16
star
53

CRAN-checks

Notes about extra CRAN checks
16
star
54

nonogram

Nonogram solver in rstats
R
15
star
55

optout

Optimized Graphics Output
R
15
star
56

ingrid

Tools for interactive grid creation and manipulation in the console
R
15
star
57

gridfont

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

phon

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

rd2list

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

flagon

Flags of the World
R
14
star
61

chipmunkbasic

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

anon

Anonymous function creation in R and purrr
R
14
star
63

devoutdrawio

R graphics device to output to draw.io XML vector format
R
14
star
64

miniverse

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

devoutaudio

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

image2xlsx

Convert an image to an excel spreadsheet
R
13
star
67

strictlyr

Stricter subset of dplyr
R
13
star
68

insitu

In-place modification of vectors
C
13
star
69

svgpatternusgs

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

cstructr

Exposing C structrs to R
C
13
star
71

ggecho

ggplot2 stat for echoing data
R
13
star
72

arcadefont

Oldschool arcade vector font
R
13
star
73

devoutrgl

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

rmonocypher

Easy to use encryption tools for R
C
13
star
75

rconnection

Writing a custom connection for R
C
13
star
76

cgrep

Highlighted grep of R objects
R
12
star
77

serializer

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

getrect

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

smallfactor

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

visvalingam

R package for Visvalingam Line Simplification
C
11
star
81

analemmatic

Create analemmatic sundials with R
R
11
star
82

cairocore

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

bdftools

Bitmap font tools for R
R
11
star
84

simplefortran

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

svgpatternsimple

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

naratext

Render text as nativeRaster images
R
10
star
87

snowcrash

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

frak

Fractal Generator
C
9
star
89

grrr

Modify function default arguments
R
9
star
90

triangular

Decompose complex polygons into sets of triangles
R
9
star
91

devoutansi

RStats ANSI graphics device
R
9
star
92

codespacer

Setup CodeSpace with RStudio support
Dockerfile
9
star
93

cryogenic

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

btnvips

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

colourlookup

Technical demonstration of hash lookup for R colour names
C
8
star
96

ggdebug

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

simplec

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

simpletrie

Simple trie in pure R
R
8
star
99

poissoned

Poisson Disc Sampling in R
R
8
star
100

devoutpdf

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