suppressPackageStartupMessages({
  library(dplyr)
  library(ggplot2)
  library(threed)
  library(ggthreed)
  library(threed)
})

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

ggthreed - 3d geoms and stats for ggplot2

Travis build status AppVeyor build status

ggthreed is a collection of ggplot2 geoms which use the threed library.

What's in the box:

Installation

You can install from github

# install.packages("devtools")
devtools::install_github("coolbutuseless/threed")
devtools::install_github("coolbutuseless/ggthreed")

Red/blue anaglyphs with stat_anaglyph()

Grab your glasses and lower your expectations!!

Usage

Example red/blue anaglyphs with points.

ggplot(mtcars) +
  geom_point(aes(mpg, y = wt, z = disp), stat = 'anaglyph', alpha = 0.5, zscale = 10) +
  theme_bw()

Example of combined 3d projection and anaglyph

library(threed)

camera_to_world <- look_at_matrix(eye = c(1.5, 1.75, 3), at = c(0, 0, 0))

obj <- threed::mesh3dobj$cube %>%
  transform_by(invert_matrix(camera_to_world)) %>%
  translate_by(c(0, 0, -3)) %>%
  perspective_projection()

ggplot(obj, aes(x, y, z = z, group = element_id)) +
  geom_polygon(fill = NA, colour='black', aes(size = hidden), stat = 'anaglyph',
               zscale = 0.05, zoffset = -1.4, zinvert = FALSE) +
  scale_linetype_manual(values = c('TRUE' = "FF", 'FALSE' = 'solid')) +
  scale_size_manual(values = c('TRUE' = 0.1, 'FALSE' = 0.5)) +
  theme_void() +
  theme(legend.position = 'none') +
  coord_equal()

Animated Anaglyph

Animated Icosahedron

See vignette('animated-anaglyph', package='ggthreed')

3d pie charts with geom_threedpie()

Usage

Issues/Limitations

Example - Simple Pie Chart

ggplot(mtcars) + 
  geom_threedpie(aes(x = as.factor(cyl))) + 
  theme_void() + 
  theme(legend.position = 'bottom')

Example - Facetted Pie Chart

ggplot(diamonds)  +
  geom_threedpie(aes(as.factor(cut))) +
  facet_wrap(~clarity, labeller = label_both) +
  labs(title = "Distribution of Diamond Cuts by Clarity") +
  scale_fill_brewer(name = "Cut", palette = 'Set2') +
  theme_void()

Example - Behind the Scenes

ggplot(mtcars) + 
  geom_threedpie(aes(x = as.factor(cyl)), alpha = 0.5) + 
  theme_void() + 
  theme(legend.position = 'bottom')

Example - Pie configuration

User adjustable:

N <- 20
start_degrees <- seq(  0, 90 , length.out = N)
tilt_degrees  <- seq(  -20, 50 , length.out = N)
heights       <- seq(0.1, 0.1, length.out = N)

start_degrees <- c(start_degrees, rev(start_degrees))
tilt_degrees  <- c(tilt_degrees , rev(tilt_degrees ))
heights       <- c(heights      , rev(heights      ))

for (i in seq_along(start_degrees)) {

  p <- ggplot(mtcars) + 
    geom_threedpie(aes(x = as.factor(cyl)), start_degrees = start_degrees[i],
                   tilt_degrees = tilt_degrees[i], height = heights[i]) + 
    theme_void() + 
    theme(legend.position = 'bottom')

  filename <- sprintf("%03i.png", i)
  print(filename)
  ggsave(filename, plot=p, width=6, height=4)

}



coolbutuseless/ggthreed documentation built on May 16, 2019, 7:14 p.m.