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
is a collection of ggplot2
geoms which use the threed
library.
What's in the box:
geom_threedpie()
for creating 3d pie charts.stat_anaglyph()
for creating red-blue anaglyph imagesYou can install from github
# install.packages("devtools") devtools::install_github("coolbutuseless/threed") devtools::install_github("coolbutuseless/ggthreed")
stat_anaglyph()
Grab your glasses and lower your expectations!!
zoffset
, zscale
- for determining how z
aesthetic influences positionred
, blue
- hex colours to use for red/blueswitch
- switch position of the coloursggplot(mtcars) + geom_point(aes(mpg, y = wt, z = disp), stat = 'anaglyph', alpha = 0.5, zscale = 10) + theme_bw()
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()
See vignette('animated-anaglyph', package='ggthreed')
geom_threedpie()
x
variable - which must be discrete.count
geom_threedpie()
overrides the aspect ratio of the plot it is displaying.
This is a gigantic hack! It means it is not possible to use coord_fixed()
etc
to change the plot aspec ratio.theme_void()
. Not sure how to disable
this from within the geom
.ggplot(mtcars) + geom_threedpie(aes(x = as.factor(cyl))) + theme_void() + theme(legend.position = 'bottom')
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()
threed
library is used to rotate the assembled polygons and perform
perspective projection.ggplot(mtcars) + geom_threedpie(aes(x = as.factor(cyl)), alpha = 0.5) + theme_void() + theme(legend.position = 'bottom')
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) }
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.