ggthreed - 3d geoms and stats for ggplot2
ggthreed is a collection
of ggplot2 geoms which use the
threed library.
What’s in the box:
geom_threedpie()for creating 3d pie charts.- I am fully aware of the crimes against visualisation I am committing here.
stat_anaglyph()for creating red-blue anaglyph images
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
- Works with point, line and polygon geoms. Although “works” may be a strong term - the 3d effect is very weak even if you tweak all the parameters
- It might work with other geoms, but no guarantees that it produces anything worth looking at.
- parameters:
zoffset,zscale- for determining howzaesthetic influences positionred,blue- hex colours to use for red/blueswitch- switch position of the colours
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
- Requires only an
xvariable - which must be discrete. - Default stat is
count - Adjustable parameters:
- camera position
- pie height
- starting angle for first pie slice
- tilt angle of pie relative to camera
Issues/Limitations
geom_threedpie()overrides the aspect ratio of the plot it is displaying. This is a gigantic hack! It means it is not possible to usecoord_fixed()etc to change the plot aspec ratio.- The initial panel grid and x- and y-axes are still generated by the
geom. Currently the only way to turn this off it to use
theme_void(). Not sure how to disable this from within thegeom. - Pie slice resolution is in increments of 2 degrees.
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
- This plot shows the polygons making up the pie.
- Triangular polygons are rendered on top, and quadrilaterals are rendered for the side.
- The
threedlibrary is used to rotate the assembled polygons and perform perspective projection. - By plotting the polygons in order of distance from the camera (furtherest polygons first), polygons which are behind others are hidden from view.
- The pie has no bottom.
- The quadrilaterals making up the side are the same colour as the top, but darkened by 15%.
ggplot(mtcars) +
geom_threedpie(aes(x = as.factor(cyl)), alpha = 0.5) +
theme_void() +
theme(legend.position = 'bottom')Example - Pie configuration
User adjustable:
- pie height
- pie tilt
- start of first pie slice







